hdu4740The Donkey of Gui Zhou dfs水题
2015-09-25 22:49
387 查看
//一头驴和一只虎在n*n的格子中跑 //它们都一直往前走,且它们不会对一个格子重复走 //它们走到不能向前走后,驴会一直向右转 //虎会一直向左转,当转了一次后还是无法向前走 //它们会待在原地 //问驴和虎最终能相遇吗 //直接dfs就行 #include<cstdio> #include<cstring> #include<iostream> using namespace std ; const int maxn = 1010 ; int vis[2][maxn][maxn] ; int dx[4] = {0 , 1 , 0 , -1} ; int dy[4] = {1 , 0 , -1 , 0} ; int nd[4] = {1, 2, 3, 0} ; int nt[4] = {3, 0, 1, 2} ; int ax , ay ; int n ; bool judge(int x , int y , int t) { if(x < 1 || x > n || y < 1 || y > n || vis[t][x][y]) return false ; return true ; } bool dfs(int ddx , int ddy , int ddd ,int tx , int ty , int ddt) { vis[0][tx][ty] = 1 ; vis[1][ddx][ddy] = 1 ; if(tx == ddx && ty == ddy) { ax = tx ;ay = ty ; return true ; } int ntx = tx + dx[ddt] ; int nty = ty + dy[ddt] ; int nddt = ddt ; int sum = 0 ; if(!judge(ntx , nty , 0)) { nddt = nt[ddt] ; ntx = tx + dx[nddt]; nty = ty + dy[nddt]; if(!judge(ntx , nty , 0)) ntx = tx , nty = ty , nddt = ddt ,sum++ ; } int nddx = ddx + dx[ddd] ; int nddy = ddy + dy[ddd] ; int nddd = ddd ; if(!judge(nddx , nddy , 1)) { nddd = nd[ddd] ; nddx = ddx + dx[nddd] ; nddy = ddy + dy[nddd] ; if(!judge(nddx , nddy , 1)) nddx = ddx , nddy = ddy , nddd = ddd , sum++ ; } if(sum == 2) return false ; return dfs(nddx , nddy , nddd , ntx , nty ,nddt) ; } int main() { while(scanf("%d" , &n) && n) { int a , b , c ; int d , e , f ; memset(vis, 0 , sizeof(vis)) ; scanf("%d%d%d%d%d%d" , &a , &b , &c , &d , &e ,&f) ; vis[0][d+1][e+1] = vis[1][a+1][b+1] = 1; if(dfs(a + 1 , b + 1, c , d + 1, e + 1 , f)) printf("%d %d\n" ,ax - 1, ay - 1); else puts("-1") ; } return 0 ; }
相关文章推荐
- UEFI 装 Debian
- HDU-1560 DNA sequence
- iOS多线程编程之NSOperation和NSOperationQueue的使用
- iOS项目开发实战——UILabel自适应较多的文本
- easyui validatebox 验证类型
- 【UIKit-125-3】#import <UIKit/UIViewController.h>
- mobile angualar ui的简单使用
- iOS项目开发实战——UIImageView的使用与图片显示模式
- AFNetWorking POST Multi-Part Request 上传图片
- UITableViewCell的移动、插入与删除
- UI高度可定制化KxMenu弹出菜单
- Android UI系列-----EditText和AutoCompleteTextView
- 黑马程序员—java技术blog—第十二篇java基础应用GUI模拟登陆注册
- UIHelp自动获取孙子节点的函数
- 云方案,依托H3C彩虹云存储架构,结合UIA统一认证系统,实现了用户数据的集中存储和管理
- n queens
- POJ 1679 The Unique MST (次小生成树)
- WAJUEJI which home strong! 1100 (广搜)
- quick-union【连通图】
- quick-find【连通图】