【算法学习笔记】88.显式DFS SJTU OJ 2202. 梅西的过人
2015-07-29 00:10
405 查看
#include <iostream> #include <stack> #include <cstdio> #include <cstring> using namespace std; int k,n,m; bool map[1000+5][1000+5]; bool vis[1000+5][1000+5]; int dx[4] = {0,0,-1,+1}; int dy[4] = {+1,-1,0,0}; void init(){ cin>>n>>m; for (int i = 1; i <= n; ++i){ for (int j = 1; j <= m; ++j){ int t; scanf("%d",&t); map[i][j] = t; } } memset(vis,0,sizeof(vis)); } struct Point { int x; int y; int done; Point(int a,int b){ x = a; y = b; done = 0; } }; bool build(){ stack<Point> s; Point start(1,1); s.push(start); while(!s.empty()){ Point cur = s.top(); s.pop(); vis[cur.x][cur.y] = true; for (int i = 0; i < 4; ++i) { int new_x = cur.x + dx[i]; int new_y = cur.y + dy[i]; if(new_x>=1 and new_x<=n and new_y>=1 and new_y<=m){ if(!vis[new_x][new_y]){ vis[new_x][new_y] = true; if(map[new_x][new_y]==false or cur.done == 0){ Point next(new_x,new_y); next.done = cur.done+map[new_x][new_y]; if(next.done <= 1){ //cout<<next.x<<","<<next.y<<endl; s.push(next); if(new_x==n and new_y==m) return true; } } } } } } return false; } int main(int argc, char const *argv[]) { cin>>k; for (int i = 0; i < k; ++i) { init(); cout<<build()<<endl; } return 0; } /* 1 3 4 0 1 0 0 1 1 1 1 0 0 1 0 */
相关文章推荐
- 值得一看
- hdu 5123 who is the best?(水)
- [HDU:5316]Magician
- AngularJS开发指南
- CodeForces 18C - Stripe
- 浅谈Java多线程中的Thread.sleep()和wait()方法的区别
- hdu 5053 the Sum of Cube(水)
- nyoj 757 期末考试 【STL 优先队列】
- 搬家啦
- 关于迭代测试的一些思考
- CodeForces 5C - Longest Regular Bracket Sequence
- 关于迭代测试的一些思考
- 开始
- 【算法学习笔记】87. 枚举路径 SJTU OJ 1999 二哥找宝藏
- cf 443B Kolya and Tandem Repeat
- HDU 1087
- poj 2135 有流量限制的最小费用最大流
- Android中滑屏实现----手把手教你如何实现触摸滑屏以及Scroller类详解
- 在matlab中进行地理坐标和像素坐标的相互转换
- 点对点 客户端-服务器 聊天程序