HDU4740-模拟
来源:互联网 发布:软件登录界面 编辑:程序博客网 时间:2024/05/16 17:26
题目:题目链接
题意:就是驴子和老虎在一个森林中跑,要是他们相遇了就输出相遇坐标,否则输出-1.跑动的原则是不跑出森林并且不跑自己跑过的路
分析:刚刚开始用一直搜索找出驴子和老虎各自走的路径,结果华丽丽的爆栈了,如下:
#include <iostream>#include <cstdio>#include <string>#include <string.h>#include <map>#include <vector>#include <cstdlib>#include <algorithm>#include <cmath>#include <queue>#include <set>#include <stack>#include <functional>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cassert>#include <bitset>#include <stack>#include <ctime>#include <list>#define INF 0x7fffffff#define max3(a,b,c) (max(a,b)>c?max(a,b):c)#define min3(a,b,c) (min(a,b)<c?min(a,b):c)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;#define maxn 1001int QuickMod(int a,int b,int n){ int r = 1; while(b) { if(b&1) r = (r*a)%n; a = (a*a)%n; b >>= 1; } return r;}int vis1[maxn][maxn];int vis2[maxn][maxn];int n;int st1x, st1y, dir1;int st2x, st2y, dir2;int fp1;int fp2;int dirx[4] = {0, 1, 0, -1};int diry[4] = {1, 0, -1, 0};bool inmap(int x, int y){ if(x < 0 || x >= n || y < 0 || y >= n) return false; return true;}int xx1, yy1;int flag;int xx2, yy2;struct node{ int x, y;} num[maxn], cop[maxn];int cntx;int cnty;void DFSX(int x1, int y1, int d1){ if(fp1) return; vis1[x1][y1] = 1; int xp = x1 + dirx[d1]; int yp = y1 + diry[d1]; if(inmap(xp, yp) && !vis1[xp][yp]) { xx1 = xp; yy1 = yp; cntx ++; num[cntx].x = xx1; num[cntx].y = yy1; DFSX(xx1, yy1, d1); } else { if(d1 == 3) { int tx = x1 + dirx[0]; int ty = y1 + diry[0]; if(!vis1[tx][ty] && inmap(tx, ty)) { xx1 = tx; yy1 = ty; cntx ++; num[cntx].x = xx1; num[cntx].y = yy1; DFSX(xx1, yy1, 0); } else fp1 = 1; } else { int tx = x1 + dirx[d1+1]; int ty = y1 + diry[d1+1]; if(!vis1[tx][ty] && inmap(tx, ty)) { xx1 = tx; yy1 = ty; cntx ++; num[cntx].x = xx1; num[cntx].y = yy1; DFSX(xx1, yy1, d1+1); } else fp1 = 1; } } if(fp1) return;}void DFSY(int x1, int y1, int d1){ if(fp2) return; vis2[x1][y1] = 1; int xp = x1 + dirx[d1]; int yp = y1 + diry[d1]; if(inmap(xp, yp) && !vis2[xp][yp]) { xx2 = xp; yy2 = yp; cnty++; cop[cnty].x = xx2; cop[cnty].y = yy2; DFSY(xx2, yy2, d1); } else { if(d1 == 0) { int tx = x1 + dirx[3]; int ty = y1 + diry[3]; if(!vis2[tx][ty] && inmap(tx, ty)) { xx2 = tx; yy2 = ty; cnty++; cop[cnty].x = xx2; cop[cnty].y = yy2; DFSY(xx2, yy2, 3); } else fp2 = 1; } else { int tx = x1 + dirx[d1-1]; int ty = y1 + diry[d1-1]; if(!vis2[tx][ty] && inmap(tx, ty)) { xx2 = tx; yy2 = ty; cnty++; cop[cnty].x = xx2; cop[cnty].y = yy2; DFSY(xx2, yy2, d1-1); } else fp2 = 1; } } if(fp2) return;}int main(){ while(scanf("%d", &n)) { if(!n) break; mem(vis1, 0); mem(vis2, 0); mem(num, 0); cntx = 0; cnty = 0; scanf("%d%d%d", &st1x, &st1y, &dir1); scanf("%d%d%d", &st2x, &st2y, &dir2); fp1 = 0; fp2 = 0; xx1 = st1x; yy1 = st1y; xx2 = st2x; yy2 = st2y; flag = 0; DFSX(st1x, st1y, dir1); DFSY(st2x, st2y, dir2); int inde = min(cntx, cnty); int i; for(i = 1; i < inde; ++i) { if(num[i].x == cop[i].x && num[i].y == cop[i].y) { flag = 1; break; } } if(flag) printf("%d %d\n", num[i].x, num[i].y); else printf("-1\n"); } return 0;}
后面就使用一步步判断的方法搞,及时跳出,这样就可以了。我又犯昨天的错误了,没有判断起点相同,WA了好几次:如下:
#include <iostream>#include <cstdio>#include <string>#include <string.h>#include <map>#include <vector>#include <cstdlib>#include <algorithm>#include <cmath>#include <queue>#include <set>#include <stack>#include <functional>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cassert>#include <bitset>#include <stack>#include <ctime>#include <list>#define INF 0x7fffffff#define max3(a,b,c) (max(a,b)>c?max(a,b):c)#define min3(a,b,c) (min(a,b)<c?min(a,b):c)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;#define maxn 1001int vis1[maxn][maxn];int vis2[maxn][maxn];int n;int st1x, st1y, dir1;int st2x, st2y, dir2;int fp1;int fp2;int dirx[4] = {0, 1, 0, -1};int diry[4] = {1, 0, -1, 0};bool inmap(int x, int y){ if(x < 0 || x >= n || y < 0 || y >= n) return false; return true;}int xx1, yy1;int flag;int xx2, yy2;void work(int x1, int y1, int d1, int x2, int y2, int d2){ while(!fp1 || !fp2) { if(!fp1) { vis1[x1][y1] = 1; int tx = x1 + dirx[d1]; int ty = y1 + diry[d1]; if(!vis1[tx][ty] && inmap(tx, ty)) { x1 = tx; y1 = ty; } else { if(d1 == 3) { tx = x1 + dirx[0]; ty = y1 + diry[0]; if(inmap(tx, ty) && !vis1[tx][ty]) { x1 = tx; y1 = ty; d1 = 0; } else fp1 = 1; } else { tx = x1 + dirx[d1+1]; ty = y1 + diry[d1+1]; if(inmap(tx, ty) && !vis1[tx][ty]) { x1 = tx; y1 = ty; d1 += 1; } else fp1 = 1; } } } if(!fp2) { vis2[x2][y2] = 1; int tx = x2 + dirx[d2]; int ty = y2 + diry[d2]; if(!vis2[tx][ty] && inmap(tx, ty)) { x2 = tx; y2 = ty; } else { if(d2 == 0) { tx = x2 + dirx[3]; ty = y2 + diry[3]; if(inmap(tx, ty) && !vis2[tx][ty]) { x2 = tx; y2 = ty; d2 = 3; } else fp2 = 1; } else { tx = x2 + dirx[d2-1]; ty = y2 + diry[d2-1]; if(inmap(tx, ty) && !vis2[tx][ty]) { x2 = tx; y2 = ty; d2 -= 1; } else fp2 = 1; } } } if(x1 == x2 && y1 == y2) { flag = 1; printf("%d %d\n", x1, y1); break; } }}int main(){ while(scanf("%d", &n)) { if(!n) break; mem(vis1, 0); mem(vis2, 0); scanf("%d%d%d", &st1x, &st1y, &dir1); scanf("%d%d%d", &st2x, &st2y, &dir2); fp1 = 0; fp2 = 0; xx1 = st1x; yy1 = st1y; xx2 = st2x; yy2 = st2y; flag = 0; if(st1x == st2x && st1y == st2y) { printf("%d %d\n", st1x, st1y); continue; } else { work(st1x, st1y, dir1, st2x, st2y, dir2); if(!flag) printf("-1\n"); } } return 0;}
唉,做了好久。。。。
- HDU4740-模拟
- hdu4740
- HDU4740
- hdu4740【杭州网赛、模拟、有点搜索?】
- HDU4740 The Donkey of Gui Zhou 暴力模拟
- hdu4740 不错的简单搜索
- hdu4740 搜索(会爆栈,需要手动开辟)
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟
- 模拟:
- 模拟
- PHP脚本调用ffmpeg对视频截图并拼接
- SQL注入漏洞全接触--高级篇
- 在Textview中显示html语句
- jeecms 2012 源码分析(1) 前台首页分析
- 直接拿来用!超实用的Java数组技巧攻略
- HDU4740-模拟
- Working Practice-破窗理论与写代码
- 说说
- 求正整数数组构成的最小数(某公司2013年校园招聘笔试试题)
- 设计模式学习
- 关于变量
- POJ 3260 The Fewest Coins(0-1背包 多重背包)
- 动态规划解最长公共子序列问题
- 【首尔大学韩国语】十七课 家里有几口人?