HDOJ 1010 解题
来源:互联网 发布:逆淘汰 知乎 编辑:程序博客网 时间:2024/06/09 18:54
HDOJ 1010解题 : http://acm.hdu.edu.cn/showproblem.php?pid=1010
在回顾搜索算法,DFS、BFS、回溯等。以HDOJ 1010题练习,虽然题目非常简单,但还是花了较长时间才AC。一开始没有剪枝,果断超时。进行了奇偶剪枝后ac。
//奇偶剪枝:如果起点横纵坐标和与终点坐标和奇偶性不同,则说明要走奇数步,如果要求走t步, //t为偶数,则肯定不可能有答案;反之亦然。
#include <iostream>#include <cstdlib>using namespace std;int n,m,t;char map[8][8];int visited[8][8];int stepx[] = {-1, 0, 1, 0};int stepy[] = {0, 1, 0, -1};int start[2];int end[2];int depth;int escape;void DFS(int x, int y, int time){ if(x<=0 || x>n || y<=0 || y>m)//防止越界 { return; } visited[x][y]=1; depth++; if(map[x][y]=='D' && depth == time) { escape = true; return; } //奇偶剪枝:如果起点横纵坐标和与终点坐标和奇偶性不同,则说明要走奇数步,如果要求走t步, //t为偶数,则肯定不可能有答案;反之亦然。 int temp=t-abs(start[0]-end[0])-abs(start[1]-end[1]); if(temp<0 || temp&1) return; for(int i=0 ; i<4 ; ++i) { if(visited[x+stepx[i]][y+stepy[i]] == 0 && !escape) { DFS(x+stepx[i], y+stepy[i],time); } else if(escape) { return; } } depth--; visited[x][y] = 0;}int main(){ while((cin >> n >> m >> t) && (n || m || t)) { escape = false; depth = -1; for(int i=0;i<=n;++i) { for(int j=0;j<=m;++j) visited[i][j] = 1; } for(int i=1 ; i<=n ; ++i) { for(int j=1 ; j<=m ; ++j) { cin >> map[i][j]; if(map[i][j] == 'S') { start[0] = i; start[1] = j; } if(map[i][j] == 'D') { end[0] = i; end[1] = j; } if(map[i][j] != 'X') { visited[i][j] = 0; } } } DFS(start[0],start[1],t); if(escape) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0;}
- HDOJ 1010 解题
- HDOJ 1020解题报告
- hdoj 1312解题报告
- hdoj 1241解题报告
- hdoj 1153 解题报告
- HDOJ-2025解题报告
- hdoj小布解题
- HDOJ-2066 解题报告
- HDOJ 1010 Tempter of the Bone解题报告
- HDOJ 1010:Tempter of the Bone 迷宫问题 解题报告
- ACM-HDoj暑假竞赛(7)-1010解题报告
- HDOJ 1008 Elevator解题报告
- HDOJ 1033 Edge解题报告
- HDOJ-1217-Arbitrage 解题报告
- HDOJ-1072-Nightmare 解题报告
- HDOJ-2571-命运 解题报告
- hdoj--1063 Exponentiation 解题报告
- HDOJ-5012-Dice 解题报告
- poj1019 Number Squence
- 使用 /sys 文件系统访问 Linux 内核
- java基础之基本算法
- python给文件写入/追加内容时输出x00和乱码的问题
- GitHub Android 开源项目汇总
- HDOJ 1010 解题
- [剑指offer][面试题37]两个链表的第一个公共节点
- UIWebView 点击获得坐标
- Android MotionEvent 坐标获取
- SendMessage函数
- USB协议
- Android中visibility属性
- jQuery获取Select option 选择的Text和Value
- Android与js交互实例