hdu1240 bfs 水题

原题链接

思路:水题,直接搜

 1 #include "map"
 2 #include "queue"
 3 #include "math.h"
 4 #include "stdio.h"
 5 #include "string.h"
 6 #include "iostream"
 7 #include "algorithm"
 8 #define abs(x) x > 0 ? x : -x
 9 #define max(a,b) a > b ? a : b
10 #define min(a,b) a < b ? a : b
11
12 using namespace std;
13
14 int z2,x2,y2,n;
15 int d[6][3]= {{0,0,1},{0,1,0},{0,0,-1},{0,-1,0},{1,0,0},{-1,0,0}};
16 bool Map[15][15][15],vis[15][15][15];
17
18 struct Node{
19     int zz,xx,yy;
20     int step;
21 };
22
23 void Bfs(int z,int x,int y)
24 {
25     memset(vis,0,sizeof(vis));
26     queue<Node>Q;
27     Node now,next;
28
29     now.zz = z;
30     now.xx = x;
31     now.yy = y;
32     now.step = 0;
33     vis[z][x][y] = 1;
34
35     Q.push(now);
36
37     while(!Q.empty())
38     {
39         now = Q.front();
40         Q.pop();
41
42         if(now.zz==z2 && now.xx==x2 && now.yy==y2)
43         {
44             printf("%d %d\n",n,now.step);
45             return;
46         }
47
48         for(int i=0; i<6; i++)
49         {
50             next.zz = now.zz + d[i][0];
51             next.xx = now.xx + d[i][1];
52             next.yy = now.yy + d[i][2];
53             next.step = now.step + 1;
54
55             if(Map[next.zz][next.xx][next.yy] && !vis[next.zz][next.xx][next.yy])
56             {
57                 vis[next.zz][next.xx][next.yy] = 1;
58                 Q.push(next);
59             }
60         }
61     }
62     printf("NO ROUTE\n");
63 }
64
65 int main()
66 {
67     int x1,y1,z1,i,j,k;
68     char s[20],c;
69     while(~scanf("%s%d",s,&n))
70     {
71         memset(Map,0,sizeof(Map));
72         for(i=1; i<=n; i++)
73             for(j=1; j<=n; j++)
74             {
75                 getchar();
76                 for(k=1; k<=n; k++)
77                 {
78                     scanf("%c",&c);
79                     if(c==‘O‘)
80                         Map[i][j][k] = 1;
81                     if(c==‘X‘)
82                         Map[i][j][k] = 0;
83                 }
84             }
85
86         scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
87         z1+=1,x1+=1,y1+=1,z2+=1,x2+=1,y2+=1;
88         getchar();
89         gets(s);
90
91         Bfs(z1,x1,y1);
92     }
93     return 0;
94 }
时间: 2024-10-13 17:17:50

hdu1240 bfs 水题的相关文章

hdu 1312 Red and Black(BFS水题)

Red and Black Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 9684    Accepted Submission(s): 6021 Problem Description There is a rectangular room, covered with square tiles. Each tile is colore

POJ1426:Find The Multiple(算是bfs水题吧,投机取巧过的)

http://poj.org/problem?id=1426 Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a c

POJ3126 Prime Path bfs, 水题 难度:0

题目 http://poj.org/problem?id=3126 题意 多组数据,每组数据有一个起点四位数s, 要变为终点四位数e, 此处s和e都是大于1000的质数,现在要找一个最短的路径把s变为e,每步可以做如下操作,把当前的s中的四位数上的某一位改变,比如1009可以变为2009,1008,1309,1049,然后检验结果是否为大于1000的质数,如果是,那就可以把s变为这个数. 思路 质数明显需要先处理出来,然后采用bfs获取结果即可. 感想 下次需要先计算空间复杂度, 1e8的空间复

POJ3278Catch That Cow(BFS+水题)

Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 64176   Accepted: 20156 Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,00

HDU1548A strange lift BFS水题

没啥好说的,注意一下,走过的楼层不能再走,否则会陷入循环 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #inclu

POJ3287(BFS水题)

Description Farmer John has been informed of the location of a fugitive cow and wants to catch her immediately. He starts at a point N (0 ≤ N ≤ 100,000) on a number line and the cow is at a point K (0 ≤ K ≤ 100,000) on the same number line. Farmer Jo

迷宫问题 BFS入门水题

1102:迷宫问题 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:84 解决: 41 题目描述 小明置身于一个迷宫,请你帮小明找出从起点到终点的最短路程. 小明只能向上下左右四个方向移动. 输入格式 输入包含多组测试数据.输入的第一行是一个整数T,表示有T组测试数据. 每组输入的第一行是两个整数N和M(1<=N,M<=100). 接下来N行,每行输入M个字符,每个字符表示迷宫中的一个小方格. 字符的含义如下: 'S':起点 'E':终点 '-':空地,可以通过 '#':障碍,无法通

4.7-4.9补题+水题+高维前缀和

题目链接:51nod 1718 Cos的多项式  [数学] 题解: 2cosx=2cosx 2cos2x=(2cosx)^2-2 2cos3x=(2cosx)^3-3*(2cosx) 数归证明2cos(nx)能表示成关于2cosx的多项式,设为f(n) f(1)=x,f(2)=x^2-2(其中的x就是2cosx) 假设n=1~k时均成立(k>=3) 当n=k+1时 由cos((k+1)x)=cos(kx)cos(x)-sin(kx)sin(x) cos((k-1)x)=cos(kx)cos(x)

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive