Max 的游戏
来源:互联网 发布:python编程工具 编辑:程序博客网 时间:2024/06/05 17:10
Description
Max is lately playing a game.Given a n﹒m board,there are only two types of grid on it:#and @. And the rule is simple:Given a start position to Max, and Max can move to four direction for each step,up,down ,left,righ.When moving between two same type grids, it costs zero, otherwise, it costs one . Now,Max gets two different positions one zhe board:the start position and the target position,he wants to know how many costs he need to spend to achieve the game at least.
Input
Input contains multiple test data sets.
For each data set,first comes two integers in one line :n,m(1<=n,m<=500),stands for the number of row and the number of column of the board.Then conmes n lines with m grid characters per each .. The characters are only @ or #. Then four integers in one line follow:x1,y1,x2,y2(0<=x1,x2<n,0<=y1,y2<m)which is the star position and the end position correspondingly.
Input is terminated by EOF.,
Output
For each test data set, one output data set should be generated as follow:
Gererates one line containing one nteger which indicates the minimum cost Max need to take for the game.
Sample Input
@#
#@
0 0 1 1
2 2
@@
@#
0 1 1 0
Sample Output
0
用了dfs的方式,不过AC不了,我的天啊,不是应该把所用的情况都应该搜索一遍吗?难道要干嘛吗?
思路:
我用了一个bfs,不过时间太长了,幸好AC了,不然会气死去。目前,还在修改运行的时间。想办法剪枝中。
代码如下:
#include <iostream>#include <cstdio>#include <string.h>#include <algorithm>#include <queue>using namespace std;int vis[505][505],si,sj,di,dj,n,m;char map[505][505];int ans;int fx[]= {0,0,-1,1},fy[]= {-1,1,0,0};struct node{ int x; int y; int step; char ch; friend bool operator <(node a,node b) { return a.step>b.step; }} s,p;void bfs(int x,int y,int step,char ch){ if(x==di && dj==y) return ; s.ch =ch; s.step = step; s.x = x; s.y = y; memset(vis,0,sizeof(vis)); priority_queue<struct node>q; vis[x][y] = 1; q.push(s); while(!q.empty()) { p = q.top(); q.pop(); for(int i=0; i<4; i++) { s.x = p.x + fx[i]; s.y = p.y + fy[i]; if(s.x<0 || s.x>=n || s.y<0 || s.y>=m || vis[s.x][s.y]==1) continue; s.ch = map[s.x][s.y]; if(s.ch != p.ch) s.step = p.step+1; else s.step = p.step; if(s.x == di && s.y==dj) { ans =min(ans,s.step); continue; } vis[s.x][s.y] = 1; q.push(s); } }}int main(){ while(scanf("%d %d",&n,&m)!=EOF) { getchar(); for(int i=0; i<n; i++) gets(map[i]); scanf("%d %d %d %d",&si,&sj,&di,&dj); ans = 99999999; bfs(si,sj,0,map[si][sj]); if(ans==99999999) printf("%d\n",0); else printf("%d\n",ans); } return 0;}
补:
求教了一位大神的思路,代码他帮我实现的,他的代码公布如下:时间少了很多。
#include <cstdio>#include <iostream>#include <algorithm>#include <vector>#include <set>#include <map>#include <string>#include <cstring>#include <stack>#include <queue>#include <cmath>#include <ctime>#include <utility>#include <cassert>using namespace std;#define REP(I,N) for (I=0;I<N;I++)#define rREP(I,N) for (I=N-1;I>=0;I--)#define rep(I,S,N) for (I=S;I<N;I++)#define rrep(I,S,N) for (I=N-1;I>=S;I--)#define FOR(I,S,N) for (I=S;I<=N;I++)#define rFOR(I,S,N) for (I=N;I>=S;I--)typedef unsigned long long ULL;typedef long long LL;const int INF=0x3f3f3f3f;const LL INFF=0x3f3f3f3f3f3f3f3fll;const LL M=1e9+7;const LL maxn=20000+7;const double eps=0.00000001;LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}template<typename T>inline T abs(T a) {return a>0?a:-a;}template<typename T>inline T powMM(T a,T b){T ret=1;for (;b;b>>=1ll,a=a*a%M) if (b&1) ret=1ll*ret*a%M;return ret;}vector<pair<int,int> > val[maxn];int n,m,s,t;char a[507][507];bool mark[507][507];int addx[]={-1,0,0,1};int addy[]={0,-1,1,0};int Q[507*507][2][2];int tot[2],now[2];int main(){while (~scanf("%d%d",&n,&m)){int i,j,k;int x0,y0,X,Y;REP(i,n) scanf("%s",a[i]);REP(i,n) REP(j,m) mark[i][j]=0;scanf("%d%d%d%d",&x0,&y0,&X,&Y);if (x0==X&&y0==Y) {puts("0");continue;}tot[0]=tot[1]=now[0]=now[1]=0;Q[tot[0]++][0][0]=x0;Q[tot[0]++][0][1]=y0;mark[x0][y0]=1;int ans=0,ANS=-1;while (1){int mark=ans&1;while (now[mark]<tot[mark]){int x=Q[now[mark]][mark][0],y=Q[now[mark]][mark][1];now[mark]++;REP(j,4){int _x=x+addx[j],_y=y+addy[j];if (_x==X&&_y==Y) ANS=ans+(a[x][y]!=a[_x][_y]);if (_x<0||_x>n-1||_y<0||_y>m-1||::mark[_x][_y]) continue;if (a[x][y]==a[_x][_y])Q[tot[mark]][mark][0]=_x,Q[tot[mark]][mark][1]=_y,tot[mark]++;elseQ[tot[mark^1]][mark^1][0]=_x,Q[tot[mark^1]][mark^1][1]=_y,tot[mark^1]++;::mark[_x][_y]=1;}if (ANS!=-1) break;}if (ANS!=-1) break;ans++;}printf("%d\n",ANS);} }
- Max 的游戏
- 基于3ds Max的游戏建模方案
- arg max 和 max的区别
- max()和数组里面的max
- Max Min的使用
- SQL2005 的VarChar(MAX)
- max带来的冲突
- max带来的冲突
- Max+Decode的妙用
- max
- MAX
- max
- max
- max
- max
- max
- 3D MAX制作游戏高端人物模型
- 基于Select Max 的分页
- ibatis 中case使用
- 高性能IO模型浅析
- 论文发表费用具体多少
- [codevs1315]摆花
- 页面兼容性
- Max 的游戏
- Netty系列四:第一个Netty程序(业务线程异步)
- ubuntu开机自动挂载新硬盘
- python魔术方法详解
- Shell-基础使用
- c# 如何弹出对话框
- 读 read
- Java中vector的使用详解
- CentOS 安装Httpie