HDU1240
来源:互联网 发布:煎饼侠 知乎 编辑:程序博客网 时间:2024/05/22 10:44
三维BFS
#include <iostream>
#include <stdio.h>#include <string.h>
#include <queue>
using namespace std;
struct node
{
int x,y,z;
int step;
} st,end;
int n,res;
char maps[15][15][15];
int move[6][3] = {{0,1,0}, {0,-1,0}, {-1,0,0}, {1,0,0}, {0,0,1}, {0,0,-1}};
bool check(node now)
{
if(now.x >= 0 && now.x < n && now.y >= 0 && now.y < n && now.z >= 0 && now.z < n)
return true;
return false;
}
int bfs()
{
node temp,now;
queue<node>que;
while(!que.empty())
que.pop();
st.step = 0;
maps[st.x][st.y][st.z] = 'X';
que.push(st);
if( st.x == end.x && st.y == end.y && st.z == end.z)
return res;
while(!que.empty())
{
temp = que.front();
que.pop();
for( int i = 0; i<6; i++)
{
now.x = temp.x + move[i][0];
now.y = temp.y + move[i][1];
now.z = temp.z + move[i][2];
if(now.x == end.x && now.y == end.y && now.z == end.z)
return temp.step + 1;
if(check(now))
{
if(maps[now.x][now.y][now.z] == 'O')
{
now.step = temp.step + 1;
//cout<<now.x<<" x "<< now.y<<" Y "<<now.z<<" Z "<<endl;
maps[now.x][now.y][now.z] = 'X';
que.push(now);
}
}
}
}
return -1;
}
int main()
{
int i,j;
char s[8],e[8];
while(scanf("%s%d",s,&n)!= EOF)
{
getchar();
for( int i = 0; i<n; i++)
{
for( int j = 0; j<n; j++)
scanf("%s", maps[i][j]);
getchar(); //一定要注意
}
scanf("%d%d%d",&st.x, &st.y, &st.z);
scanf("%d%d%d",&end.x, &end.y, &end.z);
getchar(); //一定要注意
gets(e);
res = 0;
res = bfs();
if(res >= 0)
printf("%d %d\n",n,res);
else
printf("NO ROUTE\n");
}
return 0;
}
0 0
- hdu1240
- hdu1240
- hdu1240
- HDU1240
- hdu1240
- HDU1240
- HDU1240 Asteroids!
- hdu1240 Asteroids!
- HDU1240:Asteroids!
- hdu1240 Asteroids!
- hdu1240 Asteroids!
- HDU1240:Asteroids
- hdu1240 (bfs)
- hdu1240广搜
- HDU1240:Asteroids!(BFS)
- hdu1240 Asteroids! (BFS)
- Asteroids!(hdu1240,dfs)
- hdu1240——Asteroids!
- 计算图像的拉普拉斯变换,使用方向滤波器检测边缘
- Puzzle over “goto” goto背后发生了什么
- resin 配置多个实例
- CF 421D--Bug in code
- 单纯又饥渴的IT男程序员已经成为有心机的黑木耳们诱杀的对象了!
- HDU1240
- poj 1637 Sightseeing tour(混合图欧拉回路)
- poj3436 ACM Computer Factory
- 8259A--可编程中断控制器
- opencv2.4.8+vs2010+win7环境搭建问题集合
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 26-35: ordinal not in range(12
- STL_Map用法详解
- JBPM流程实例(PV)Process Variables
- Ubuntu下使用Vi是方向键变乱码 退格键不能使用的解决方法