hdu1010 dfs+剪枝
来源:互联网 发布:手机移动数据怎么关闭 编辑:程序博客网 时间:2024/05/16 16:11
剪枝很巧妙的,对于下标相加为奇数的点不能在偶数步走到下标相加为偶数的点,依次类推
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;char map[10][10];bool book[10][10];int to[4][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};int startx, starty, endx, endy;int N, M, T, flag, temp, s1, s2;void dfs(int x, int y){if(flag){return;}int nowx, nowy;if(x == endx && y == endy){if(!T)flag = 1;}if(!T){return;}for(int i = 0; i < 4; i++){nowx = x + to[i][0];nowy = y + to[i][1];if(nowx > 0 && nowx <= N && nowy > 0 && nowy <= M){if((map[nowx][nowy] == '.' || map[nowx][nowy] == 'D') && !book[nowx][nowy]){T--;book[x][y] = true;dfs(nowx, nowy);book[x][y] = false;T++;}}}}int main(){while(scanf("%d %d %d", &N, &M, &T) != EOF){temp = -1;if(!N || !M || !T){return 0;}getchar();flag = 0;memset(book, 0, sizeof(book));for(int i = 1; i <= N; i++){for(int j = 1; j <= M; j++){scanf("%c", &map[i][j]);if(map[i][j] == 'S'){startx = i;starty = j;}if(map[i][j] == 'D'){endx = i;endy = j;}}getchar();}if(abs(startx-endx)+abs(starty-endy)>T||(startx+endx+starty+endy+T)%2==1) //剪枝 { printf("NO\n"); continue; }dfs(startx, starty);if(flag){cout << "YES" << endl;}else{cout << "NO" << endl;}}return 0;}
0 0
- hdu1010 DFS和剪枝
- HDU1010-奇偶剪枝(DFS)
- hdu1010 dfs+路径剪枝
- hdu1010(dfs+剪枝)
- hdu1010 dfs奇偶剪枝
- HDU1010奇偶剪枝DFS
- hdu1010 dfs+剪枝
- HDU1010 DFS+奇偶剪枝
- dfs+剪枝(hdu1010)
- hdu1010 dfs+剪枝
- HDU1010 dfs奇偶剪枝
- HDU1010 DFS+奇偶剪枝
- hdu1010(dfs+剪枝)
- hdu1010(DFS + 奇偶剪枝)
- hdu1010(DFS+奇偶剪枝)
- hdu1010 (dfs+奇偶剪枝)
- hdu1010(dfs加剪枝)
- HDU1010 DFS+剪枝水题
- DFS——HDU1010(经典的奇偶剪枝)
- 转载:关于函数返回值的几种情况
- L3-005. 垃圾箱分布
- Linux下安装jdk(openSUSE为例)
- 5.CAS增加验证码
- hdu1010 dfs+剪枝
- window 命令行运行的java 和 javac 显示的版本不同
- Quartz入门例子简介 从入门到菜鸟(四)
- MySQL 手动设置自动递增伟某一值
- UGUI背包(二)鼠标移动会显示信息框,拖拽物品
- 细说final关键字和static关键字
- PAT(A) - 1103. Integer Factorization (30)
- 函数调用和返回过程栈帧结构变化图(C语言举例)
- ubuntu科学软件