1010.Tempter of the Bone
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it up, the maze began to shake, and the doggie could feel the ground sinking. He realized that the bone was a trap, and he tried desperately to get out of this maze.
The maze was a rectangle with sizes N by M. There was a door in the maze. At the beginning, the door was closed and it would open at the T-th second for a short period of time (less than 1 second). Therefore the doggie had to arrive at the door on exactly the T-th second. In every second, he could move one block to one of the upper, lower, left and right neighboring blocks. Once he entered a block, the ground of this block would start to sink and disappear in the next second. He could not stay at one block for more than one second, nor could he move into a visited block. Can the poor doggie survive? Please help him.
Input
The input consists of multiple test cases. The first line of each test case contains three integers N, M, and T (1 < N, M < 7; 0 < T < 50), which denote the sizes of the maze and the time at which the door will open, respectively. The next N lines give the maze layout, with each line containing M characters. A character is one of the following:
'X': a block of wall, which the doggie cannot enter;
'S': the start point of the doggie;
'D': the Door; or
'.': an empty block.
The input is terminated with three 0's. This test case is not to be processed.
Output
For each test case, print in one line "YES" if the doggie can survive, or "NO" otherwise.
Sample Input
4 4 5
S.X.
..X.
..XD
....
3 4 5
S.X.
..X.
...D
0 0 0
Sample Output
NO
YES
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,m,k;
int x2,y2;
char map[9][9];
bool v[9][9];
int jx[]={0,1,0,-1};
int jy[]={1,0,-1,0};
int pf,p;
void DFS(int xx,int yy,int cnt)
{
if(xx==x2&&yy==y2&&cnt==k)
{
pf=1;
return ;
}
if(pf==1)
return ;
for(int i=0;i<4;i++)
{
int fx=xx+jx[i];
int fy=yy+jy[i];
int ans=abs(x2-fx)+abs(y2-fy);
if((1+cnt+ans<=k)&&(1+cnt+ans)%2==p&&fx>=0 && fx<n && fy>=0 && fy<m && map[fx][fy]!='X' && v[fx][fy] == 0)
{
v[fx][fy]=1;
DFS(fx,fy,cnt+1);
v[fx][fy]=0;
}
}
}
int main()
{
while(scanf("%d%d%d",&n,&m,&k)!=EOF)
{
memset(v,0,sizeof(v));
if(n==0&&m==0&&k==0)
break;
p=k%2;
pf=0;
int x1,y1;
for(int i=0;i<n;i++)
{
getchar();
for(int j=0;j<m;j++)
{
scanf("%c",&map[i][j]);
if(map[i][j]=='S')
{
x1=i;
y1=j;
}
else if(map[i][j]=='D')
{
x2=i;
y2=j;
}
}
}
getchar();
int pans=abs(y2-y1)+abs(x2-x1);
if(pans>k||pans%2!=p)
{
printf("NO
");
continue;
}
v[x1][y1]=1;
DFS(x1,y1,0);
if(pf==1)
printf("YES
");
else
printf("NO
");
}
return 0;
}
1240.Asteroids!
You're in space.
You want to get home.
There are asteroids.
You don't want to hit them.
Input
Input to this problem will consist of a (non-empty) series of up to 100 data sets. Each data set will be formatted according to the following description, and there will be no blank lines separating data sets.
A single data set has 5 components:
Start line - A single line, "START N", where 1 <= N <= 10.
Slice list - A series of N slices. Each slice is an N x N matrix representing a horizontal slice through the asteroid field. Each position in the matrix will be one of two values:
'O' - (the letter "oh") Empty space
'X' - (upper-case) Asteroid present
Starting Position - A single line, "A B C", denoting the <A,B,C> coordinates of your craft's starting position. The coordinate values will be integers separated by individual spaces.
Target Position - A single line, "D E F", denoting the <D,E,F> coordinates of your target's position. The coordinate values will be integers separated by individual spaces.
End line - A single line, "END"
The origin of the coordinate system is <0,0,0>. Therefore, each component of each coordinate vector will be an integer between 0 and N-1, inclusive.
The first coordinate in a set indicates the column. Left column = 0.
The second coordinate in a set indicates the row. Top row = 0.
The third coordinate in a set indicates the slice. First slice = 0.
Both the Starting Position and the Target Position will be in empty space.
Output
For each data set, there will be exactly one output set, and there will be no blank lines separating output sets.
A single output set consists of a single line. If a route exists, the line will be in the format "X Y", where X is the same as N from the corresponding input data set and Y is the least number of moves necessary to get your ship from the starting position to the target position. If there is no route from the starting position to the target position, the line will be "NO ROUTE" instead.
A move can only be in one of the six basic directions: up, down, left, right, forward, back. Phrased more precisely, a move will either increment or decrement a single component of your current position vector by 1.
Sample Input
START 1 O 0 0 0 0 0 0 END START 3 XXX XXX XXX OOO OOO OOO XXX XXX XXX 0 0 1 2 2 1 END START 5 OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO XXXXX XXXXX XXXXX XXXXX XXXXX OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO OOOOO 0 0 0 4 4 4 END
Sample Output
1 0 3 4 NO ROUTE
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct point
{
int l, x, y, step;
point(int ll, int xx, int yy, int st)
:l(ll), x(xx), y(yy), step(st)
{
}
};
int n, maze[10][10][10];
// 起始和终点
int bx, by, bz;
int ex, ey, ez;
char op[15];
// 增量
int ud[] = {1, -1, 0, 0, 0, 0};
int lr[] = {0, 0, 1, 0, -1, 0};
int fb[] = {0, 0, 0, 1, 0, -1};
void input()
{
scanf("%d", &n);
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
scanf("%s", op);
for(int k=0; k<n; k++)
{
if(op[k]=='O') maze[i][j][k] = 0;
else maze[i][j][k] = 1;
}
}
}
scanf("%d%d%d%d%d%d", &by, &bx, &bz, &ey, &ex, &ez);
scanf("%s", op);
}
bool judge(int l, int x, int y)
{
// 判越界和可行
if(maze[l][x][y]==1) return false;
if(l>=n||l<0) return false;
if(x>=n||x<0) return false;
if(y>=n||y<0) return false;
return true;
}
void dfs()
{
queue<point> qu;
qu.push(point(bz, bx, by, 0));
while(!qu.empty())
{
point& tmp = qu.front();
int cur[] = {tmp.l, tmp.x, tmp.y, tmp.step};
if(cur[0]==ez&&cur[1]==ex&&cur[2]==ey)
{
printf("%d %d
", n, cur[3]);
return;
}
else
{
qu.pop();
for(int i=0; i<6; i++)
{
int l = cur[0]+ud[i];
int x = cur[1]+fb[i];
int y = cur[2]+lr[i];
if(judge(l, x, y))
{
maze[l][x][y] = 1;
qu.push(point(l, x, y, cur[3]+1));
}
}
}
}
printf("NO ROUTE
");
}
int main()
{
while(scanf("%s", op)!=EOF)
{
input();
dfs();
}
return 0;
}