2013
11-12

# Children of the Candy Corn

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit.

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there’s no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn’t work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.)

As the proprieter of a cornfield that is about to be converted into a maze, you’d like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'.

Exactly one ‘S’ and one ‘E’ will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls (‘#’), with the only openings being the ‘S’ and ‘E’. The ‘S’ and ‘E’ will also be separated by at least one wall (‘#’).

You may assume that the maze exit is always reachable from the start point.

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the ‘S’ and ‘E’) for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

37 5 5
17 17 9

/* @author: */
import java.util.*;
public class Main
{
static String map[] = new String[50];
static int q[] = new int[2000];
static int n, m;
static boolean inmap( int x, int y ) {
return 0<=x&&x< n && 0<=y&&y< m;
}

static boolean sign[][] = new boolean[50][50];
static final int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, 1, 0, -1 };

static int search( int x, int y, int towards, int key ) {
int xx, yy;
if( map[x].charAt(y) == 'E' )
return 1;
//	System.out.print( x + " " + y + "\n" );
for( int i=towards+4-key; ;i+=key )
if( inmap(xx=x+dx[i&3],yy=y+dy[i&3]) && map[xx].charAt(yy) != '#' )
return search( xx, yy, i&3, key ) + 1;
}

static int shortest( int x, int y ) {
int i, j, qh, qt, xx, yy;
for( i=0; i< n; i++ )
for( j=0; j< m; j++ )
sign[i][j] = false;
qh = 0;
qt = 2;
q[0] = (x<< 8)+y;
q[1] = -1;
sign[x][y] = true;

int count = 2;
while( true ) {
int c = q[qh++];
if( c < 0 ) {
count++;
q[qt++] = c;
continue;
}
x = c>>8;
y = ( c & ((1<<8)-1) );
for( i=0; i< 4; i++ )
if( inmap(xx=x+dx[i],yy=y+dy[i]) && map[xx].charAt(yy) != '#' && !sign[xx][yy] ) {
q[qt++] = (xx<<8) + yy;
if( map[xx].charAt(yy) == 'E' )
return count;
sign[xx][yy] = true;
}
}

}

public static void main(String args[])
{
int t, j, i;
Scanner cin = new Scanner( System.in );
t = cin.nextInt();
while( t-- != 0 ) {
m = cin.nextInt();
n = cin.nextInt();
for( i=0; i< n; i++ )
map[i] = cin.next();
loop: for( i=0; i< n; i++ ) {
for( j=0; j< m; j++ )
if( map[i].charAt( j ) == 'S' ) {
System.out.println( search( i, j, 0, -1 ) + " " + search( i, j, 0, 1 )
+ " " + shortest(i,j) );
break loop;
}
}
}
return;
}
}

1. A猴子认识的所有猴子和B猴子认识的所有猴子都能认识，这句话用《爱屋及乌》描述比较容易理解……

2. #include <stdio.h>
int main()
{
int n,p,t[100]={1};
for(int i=1;i<100;i++)
t =i;
while(scanf("%d",&n)&&n!=0){
if(n==1)
printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
else {
if(n%4) p=n/4+1;
else p=n/4;
int q=4*p;
printf("Printing order for %d pages:n",n);
for(int i=0;i<p;i++){
printf("Sheet %d, front: ",i+1);
if(q>n) {printf("Blank, %dn",t[2*i+1]);}
else {printf("%d, %dn",q,t[2*i+1]);}
q–;//打印表前
printf("Sheet %d, back : ",i+1);
if(q>n) {printf("%d, Blankn",t[2*i+2]);}
else {printf("%d, %dn",t[2*i+2],q);}
q–;//打印表后
}
}
}
return 0;
}

3. 网站做得很好看，内容也多，全。前段时间在博客园里看到有人说：网页的好坏看字体。觉得微软雅黑的字体很好看，然后现在这个网站也用的这个字体！nice!

4. 在方法1里面：

//遍历所有的边，计算入度
for(int i=0; i<V; i++)
{
degree = 0;
for (j = adj .begin(); j != adj .end(); ++j)
{
degree[*j]++;
}
}

为什么每遍历一条链表，要首先将每个链表头的顶点的入度置为0呢？
比如顶点5，若在顶点1、2、3、4的链表中出现过顶点5，那么要增加顶点5的入度，但是在遍历顶点5的链表时，又将顶点5的入度置为0了，那之前的从顶点1234到顶点5的边不是都没了吗？