下午1点,继续写地牢题。简单bfs。
Dungeon Master -UVA - 532
[NWUACM]
你被困在一个三维的空间中,现在要寻找最短路径逃生!
空间由立方体单位构成
你每次向上下前后左右移动一个单位需要一分钟
你不能对角线移动并且四周封闭
是否存在逃出生天的可能性?如果存在,则需要多少时间?
输入第一行是一个数表示空间的数量。
每个空间的描述的第一行为L,R和C(皆不超过30)。
L表示空间的高度。
R和C分别表示每层空间的行与列的大小。
随后L层地牢,每层R行,每行C个字符。
每个字符表示空间的一个单元。'#'表示不可通过单元,'.'表示空白单元。你的起始位置在'S',出口为'E'。
每层空间后都有一个空行。L,R和C均为0时输入结束。
每个空间对应一行输出。
如果可以逃生,则输出如下
Escaped in x minute(s).
x为最短脱离时间。
如果无法逃生,则输出如下
Trapped!
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0Sample Output - 输出样例
Escaped in 11 minute(s). Trapped!一开始把结构体的顺序写错了,然后就一直卡在为什么这个点入不了队。。
然后有个WA点是测完一组数据后要把队列清空。
代码写的比较乱。。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<queue>
using namespace std;
struct coord
{
int z;
int x;
int y;
int t;
};
int bfs(int z, int x, int y);
char maze[32][32][32];
bool jianzhi[31][31][31];
queue<coord> que;
int dx[6] = {1,-1,0,0,0,0},dy[6] = {0,0,1,-1,0,0}, dz[6] = {0,0,0,0,1,-1};
int x = 1,y = 1,z = 1;
int main()
{
std::ios::sync_with_stdio(false);
while(x && y && z)
{
memset(maze,0,sizeof(maze));
memset(jianzhi,0,sizeof(jianzhi));
int a,b,c;
cin >> z >> x >> y;
if (x == 0 && y == 0 && z == 0)
break;
for (int i = 1; i <= z; i++)
{
for (int j = 1; j <= x; j++)
{
for (int k = 1; k <= y; k++)
{
cin >> maze[i][j][k];
if (maze[i][j][k] == 'S')
{
a = i;
b = j;
c = k;
}
}
}
}
int ans = bfs(a,b,c);
if(ans == -1)
cout << "Trapped!\n";
else
cout << "Escaped in " << ans << " minute(s).\n";
}
return 0;
}
int bfs(int q, int w, int e)
{
//if (maze[z][x][y] == '#')
struct coord t = {q,w,e,0};
que.push(t);
while (que.size())
{
//cout << que.size() <<endl;
t = que.front();
//cout << t.z << " " << t.x << " " << t.y << " " << t.t << endl;
que.pop();
for (int i = 0; i < 6; i++)
{
if (0 < t.z+dz[i] && t.z+dz[i] <= z && 0<t.x+dx[i] && t.x+dx[i] <= x && 0<t.y+dy[i] && t.y+dy[i]<=y)
{
if (maze[t.z+dz[i]][t.x+dx[i]][t.y+dy[i]] == 'E')
{
while (que.size())
que.pop();
return t.t + 1;
}
if (maze[t.z+dz[i]][t.x+dx[i]][t.y+dy[i]] == '.' && jianzhi[t.z+dz[i]][t.x+dx[i]][t.y+dy[i]] == 0)
{
//cout << t.z+dz[i] << " " << t.x+dx[i] << " " << t.y+dy[i] << " " << maze[t.z+dz[i]][t.x+dx[i]][t.y+dy[i]] << endl;
struct coord m = {t.z+dz[i],t.x+dx[i],t.y+dy[i],t.t + 1};
que.push(m);
jianzhi[t.z+dz[i]][t.x+dx[i]][t.y+dy[i]] = 1;
}
}
}
}
while (que.size())
que.pop();
return -1;
}
nwu17冬-03[搜索大专题1] B题
简单dfs,基本就是挑程p32的那道题,不贴代码了。
C题 做过。。bfs+剪枝 WA点在于数据范围
D题
A robot has been programmed to follow the instructions in its path. Instructions for the next direction the robot is to move are laid down in a grid. The possible instructions are
N north (up the page)
S south (down the page)
E east (to the right on the page)
W west (to the left on the page)
For example, suppose the robot starts on the north (top) side of Grid 1 and starts south (down). The path the robot follows is shown. The robot goes through 10 instructions in the grid before leaving the grid.
Compare what happens in Grid 2: the robot goes through 3 instructions only once, and then starts a loop through 8 instructions, and never exits.
You are to write a program that determines how long it takes a robot to get out of the grid or how the robot loops around.
There will be one or more grids for robots to navigate. The data for each is in the following form. On the first line are three integers separated by blanks: the number of rows in the grid, the number of columns in the grid, and the number of the column in which the robot enters from the north. The possible entry columns are numbered starting with one at the left. Then come the rows of the direction instructions. Each grid will have at least one and at most 10 rows and columns of instructions. The lines of instructions contain only the characters N, S, E, or W with no blanks. The end of input is indicated by a row containing 0 0 0.
Output
For each grid in the input there is one line of output. Either the robot follows a certain number of instructions and exits the grid on any one the four sides or else the robot follows the instructions on a certain number of locations once, and then the instructions on some number of locations repeatedly. The sample input below corresponds to the two grids above and illustrates the two forms of output. The word "step" is always immediately followed by "(s)" whether or not the number before it is 1.
Sample Input
3 6 5 NEESWE WWWESS SNWWWW 4 5 1 SESWE EESNW NWEEN EWSEN 0 0 0Sample Output
10 step(s) to exit 3 step(s) before a loop of 8 step(s)
dfs。多加一个判断条件。
#include <iostream>
#include <cstring>
using namespace std;
int robot (int x,int y);
char room[12][12];
int mark[12][12];
int row,column,entrance,res,step;
int main()
{
while (cin >> row >> column >> entrance)
{
res = 0;
step = 0;
memset(mark,0,sizeof(mark));
if (row == 0 && column == 0 && entrance == 0)
break;
for (int i = 1; i <= row; i++)
for(int j = 1; j <= column; j++)
{
cin >> room[i][j];
}
robot(1,entrance);
if (res == 0)
cout << step - 1<< " step(s) to exit\n";
else
cout << step - res << " step(s) before a loop of " << res << " step(s)\n";
}
}
int robot (int x,int y)
{
if (mark[x][y] == 0)
step++;
if (mark[x][y] == 1)
res++;
if (mark[x][y] == 2)
return 0;
if (x > 0 && x <= row && y > 0 && y <= column)
{
mark[x][y]++;
if (room[x][y] == 'N')
robot(x-1,y);
if (room[x][y] == 'S')
robot(x+1,y);
if (room[x][y] == 'W')
robot(x,y-1);
if (room[x][y] == 'E')
robot(x,y+1);
}
return 0;
}
明日计划。
看书。紫书看到第7章,顺便刷题。晚上打CF。