hdu 2531 Catch him (bfs)
2015-10-12 13:12
399 查看
Catch him
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 664 Accepted Submission(s): 307
[align=left]Problem Description[/align]
在美式足球中,四分卫负责指挥整只球队的进攻战术和跑位,以及给接球员传球的任务。四分卫是一只球队进攻组最重要的球员,而且一般身体都相对比较弱小,所以通常球队会安排5-7名大汉来保护他,其中站在四分卫前方、排成一线的5名球员称为进攻锋线,他们通常都是135公斤左右的壮汉。
/************************************************************************* > File Name: code/hdu/2531.cpp > Author: 111qqz > Email: rkz2013@126.com > Created Time: 2015年10月11日 星期日 23时14分43秒 ************************************************************************/ #include<iostream> #include<iomanip> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> #include<map> #include<set> #include<queue> #include<vector> #include<stack> #include<cctype> #define yn hez111qqz #define j1 cute111qqz #define ms(a,x) memset(a,x,sizeof(a)) using namespace std; const int dx4[4]={1,0,0,-1}; const int dy4[4]={0,-1,1,0}; typedef long long LL; typedef double DB; const int inf = 0x3f3f3f3f; const int N=1E2+5; int w,h; char maze ; bool v ; int cnt ; int fx[30]; int fy[30]; struct node { int x,y; int d; bool ok() { if (x<0||y<0||x>=w||y>=h) return false; if (maze[x][y]=='O') return false; if (v[x][y]) return false; for ( int i =2 ; i <= cnt ; i++) { int xx = x + fx[i]; int yy = y + fy[i]; if (xx<0||yy<0||xx>=w||yy>=h) return false; if (maze[xx][yy]=='O') return false; } return true; } bool get() { if (maze[x][y]=='Q') return true; for ( int i = 2 ; i <= cnt ; i++) { int xx = x + fx[i]; int yy = y + fy[i]; if (maze[xx][yy]=='Q') { return true; } } return false; } }s; bool bfs() { queue<node>q; q.push(s); while (!q.empty()) { node pre = q.front();q.pop(); // printf("x: %d y: %d d: %d \n",pre.x,pre.y,pre.d); if (pre.get()) { printf("%d\n",pre.d); return true; } for ( int i = 0 ; i < 4 ; i++) { node next; next.x = pre.x + dx4[i]; next.y = pre.y + dy4[i]; next.d = pre.d + 1; if (next.ok()) { v[next.x][next.y] = true; q.push(next); } } } return false; } int main() { #ifndef ONLINE_JUDGE freopen("in.txt","r",stdin); #endif while (scanf("%d %d",&w,&h)!=EOF&&w&&h) { ms(v,false); cnt = 0 ; for ( int i = 0 ; i < w ; i++) scanf("%s",maze[i]); for ( int i = 0 ; i < w ; i++) for ( int j = 0 ; j < h ; j++) { if (maze[i][j]=='D') { cnt++; if (cnt==1) { fx[cnt] = i; fy[cnt] = j; s.x = i; s.y = j; s.d = 0 ; v[i][j] = true; } else { fx[cnt] = i - fx[1]; //相对位移. fy[cnt] = j - fy[1]; } } } if (!bfs()) { puts("Impossible"); } } #ifndef ONLINE_JUDGE fclose(stdin); #endif return 0; }
View Code
相关文章推荐
- iOS ---属性传值--正向界面传值
- 数据挖掘方法论及实施步骤
- HTTP协议-HTTP权威指南
- HDU 4578 Transformation
- UIWebView的基本用法
- 全面理解http
- 【UWP】UI适配整理
- Linux改变进程优先级的nice及renice命令
- 猫抓老鼠 floyd SRM 670 Div2 Problem 1050 - Treestrat
- eclipse出现: Android sdk content loader 0%
- HDU 4497 GCD and LCM
- 2015年网站设计我们将看到这十大趋势
- 添加登陆Dlg
- 奇酷手机显示Log
- 数据结构与算法-线性表顺序存储结构删除操作的实现
- Java基础:X+=Y和X=X+Y的区别
- Android UI线程与子线程
- 论好的翻译
- 数据结构与算法-线性表顺序存储结构删除操作的实现
- android 支付宝开发 6001错误解决方法