Hdu 1072 【广搜】.cpp
題意:
給出一個(gè)n*m的矩陣,
0 表示不可走
1 表示可走
2 表示起點(diǎn)
3 表示終點(diǎn)
4 表示可走且走到這一步可以滿(mǎn)血
?
某人一開(kāi)始有6滴血,走一步少一滴..到0就死了..
可以走到4的位置滿(mǎn)血再走..
求出最少走幾步可以從起點(diǎn)到終點(diǎn)..
思路:
廣搜,用一個(gè)cnt表示現(xiàn)在走了幾步,一個(gè)cur表示現(xiàn)在的血量..
普通的廣搜過(guò)程中如果已經(jīng)走過(guò)就不可以再走了..但是第三個(gè)樣例可以看出走過(guò)還可以再走..
因?yàn)槠胀◤V搜走過(guò)不能再走只是代表這個(gè)狀態(tài)已經(jīng)訪(fǎng)問(wèn)過(guò)了..不需要再次訪(fǎng)問(wèn),防止死循環(huán)..
但是現(xiàn)在再次訪(fǎng)問(wèn)的時(shí)候因?yàn)檠恳呀?jīng)不一樣了.所以可以再走..
剩下就是正常廣搜了..
?
Tips:
404..
?
Code:
1 #include <stdio.h> 2 #include <cstring> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 7 const int MAXN = 10; 8 const int INF = 0x1f1f1f1f; 9 10 struct Node 11 { 12 int x; 13 int y; 14 int cnt; 15 int cur; 16 }; 17 18 int dir[4][2] = {0, 1, 1, 0, 0, -1, -1, 0}; 19 int iCase, n, m; 20 int G[MAXN][MAXN], vis[MAXN][MAXN], dis[MAXN][MAXN]; 21 22 bool check(Node node) 23 { 24 return node.x >= 0 && node.x < n && node.y >= 0 && node.y < m && (!vis[node.x][node.y] || dis[node.x][node.y] < node.cur) && node.cur != 0; 25 } 26 27 /* 28 0 wall 29 1 nothing 30 2 start 31 3 end 32 4 reset 33 */ 34 35 int main() 36 { 37 // freopen("in.txt", "r", stdin); 38 bool flag; 39 Node ed, node, node1; 40 int ans; 41 scanf("%d", &iCase); 42 while (iCase--) { 43 memset(vis, false, sizeof(vis)); 44 memset(dis, INF, sizeof(dis)); 45 flag = false; 46 queue<Node> Q; 47 scanf("%d %d", &n, &m); 48 for (int i = 0; i < n; ++i) 49 for (int j = 0; j < m; ++j) { 50 scanf("%d", &G[i][j]); 51 if (G[i][j] == 2) { 52 node.x = i, node.y = j; 53 node.cnt = 0; 54 node.cur = 6; 55 Q.push(node); 56 dis[i][j] = node.cur; 57 vis[i][j] = true; 58 } else if (G[i][j] == 3) 59 ed.x = i, ed.y = j; 60 else if (G[i][j] == 0) 61 vis[i][j] = true; 62 } 63 while (!Q.empty()) { 64 node = Q.front(); 65 if (node.x == ed.x && node.y == ed.y) { 66 ans = node.cnt; 67 flag = true; 68 break; 69 } 70 Q.pop(); 71 for (int i = 0; i < 4; ++i) { 72 node1.x = node.x+dir[i][0]; 73 node1.y = node.y+dir[i][1]; 74 node1.cnt = node.cnt+1; 75 node1.cur = node.cur-1; 76 if (check(node1)) { 77 if (G[node1.x][node1.y] == 4) node1.cur = 6; 78 dis[node1.x][node1.y] = node.cur; 79 Q.push(node1); 80 vis[node1.x][node1.y] = true; 81 } 82 } 83 } 84 if (flag) printf("%d\n", ans); 85 else puts("-1"); 86 } 87 return 0; 88 } View Code?
鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1072
轉(zhuǎn)載于:https://www.cnblogs.com/Griselda/archive/2013/06/06/3122681.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的Hdu 1072 【广搜】.cpp的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: sql中contains,like,pa
- 下一篇: 图的根节点-数据结构作业。。