hdu 1072 Nightmare
2013-10-25 20:32
309 查看
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; /* 思路: 当遇到4时,只走一次, 广搜就行了; */ int num[10][10]; struct node { int x, y; int t; int step; }; int T, n, m; int sx, sy; int ex, ey; int ans; int dir[][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; bool judge(int x, int y) { if(x >= 1 && x <= n && y >= 1 && y <= m && num[x][y] != 0) return true; return false; } void bfs() { node cur, next; cur.x = sx; cur.y = sy; cur.t = 6; cur.step = 0; queue<node> que; que.push(cur); while(!que.empty()) { cur = que.front(); que.pop(); if(cur.x == ex && cur.y == ey) { ans = cur.step; break; } for(int i = 0; i < 4; i++) { int a = cur.x + dir[i][0]; int b = cur.y + dir[i][1]; if(judge(a, b)&& cur.t > 1) { next.x = a; next.y = b; next.step = cur.step + 1; next.t = cur.t - 1; if(num[a][b] == 4) { num[a][b] = 0; next.t = 6; } que.push(next); } } } } int main() { scanf("%d", &T); while(T--) { scanf("%d%d", &n, &m); for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { scanf("%d", &num[i][j]); if(num[i][j] == 2) { sx = i; sy = j; } if(num[i][j] == 3) { ex = i; ey = j; } } } ans = -1; bfs(); printf("%d\n", ans); } return 0; }
相关文章推荐
- 贝叶斯公式和全概率公式--笔试中的那些有趣的数学问题
- C++友元函数(类)用法
- 回型矩阵 简述与java语言实现
- C#的方法采用默认的参数时应注意版本问题
- hdoj 湫湫系列故事——减肥记I
- android webkit HTML5 video播放
- java数据库连接(二)
- Linux中自定义系统调用
- UVa247 - Calling Circles
- 网络基础的基础
- activitygroup里被管理的所有activity的onactivityresult失效问题
- java数据库连接(一)
- trap与INT问题
- 【日常】ICS的lab7攻略和最近生活的吐槽
- 设计模式
- 流行的一些技术术语
- 合并两个单链表练习,多项式合并
- mysql基于mysqlslap的压力测试
- 好累啊
- Linux shell脚本