UVa1600做题总结
2015-05-31 15:03
218 查看
用一个State三维数组存储当前位置的状态以防止以后重复走又可以对该位置更新值。
#include<cstdio> #include<cstring> #include<algorithm> #include<map> #include<stack> #include<queue> #include<set> #include<vector> #include<cmath> using namespace std; #define MAXD 20 + 10 int n,m,k; int Mat[MAXD][MAXD]; int State[21][21][21]; /*走到x,y 连穿z个墙*/ int ans; int dir[4][2] = {{-1,0},{1,0},{0,-1},{0,1}}; void dfs(int x,int y,int step,int have_go){ if(x == n - 1 && y == m - 1){ ans = min(ans,step); return ; } for(int i = 0 ; i < 4 ; i++){ int _x = x + dir[i][0]; int _y = y + dir[i][1]; int st = have_go; if(Mat[_x][_y] == 1) st++; else st = 0; if(_x >= 0 && _x < n && _y >= 0 && _y < m){ if((State[_x][_y][st] < 0 || State[_x][_y][st] > step + 1) && st <= k){ State[_x][_y][st] = step + 1; dfs(_x,_y,step + 1,st); } } } return ; } int main(){ int T; scanf("%d",&T); while(T--){ memset(State,-1,sizeof(State)); ans = 1 << 30; scanf("%d%d%d",&n,&m,&k); for(int i = 0 ; i < n ; i++) for(int j = 0 ; j < m ; j ++) scanf("%d",&Mat[i][j]); dfs(0,0,0,0); /*从左上角出发,当前走了0补,已经连续穿过了0层墙*/ if(ans != 1 << 30)printf("%d\n",ans); else printf("-1\n"); } return 0; }
相关文章推荐
- 十二周——多态性——有阅读程序(3.2)
- 【操作系统总结】进程通信
- WinsockPragramming(2) Client(C++版)
- JAVA 文档注释,类的说明,HTML说明文档的生成
- [leedcode219]Contains Duplicate II
- 一个女运维学生写给老男孩老师的感谢信!
- 一个女运维学生写给老男孩老师的感谢信!
- 研究android系统架构层次
- 设置MyEclipse的代码自动提示功能
- 11gR2 集群(CRS/GRID)新功能——SCAN(Single Client Access Name)
- 个人认为讲解java异常最好的文章
- 第十三周项目二 形状类族中的纯虚函数
- 常量指针和指针常量
- 给一个整数数列,修改最少的数使得严格递增
- WinsockPragramming(1) Server(C++版)
- 十二周——多态性——阅读程序(3)
- UILable的属性及方法使用
- VMware Workstation 不可恢复错误:(vmui)
- Internal Sorting: Heapsort-2:Sorting by Selection
- 菜鸟学Android笔记(十二):Tomcat的安装配置