游戏
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> using namespace std; struct Node { int t1,t2; }node[105][105]; int n,m,mint=100000; int vis[105][105]; //因为角色必须移动,所以玩家在某个点碰壁后需要往回走(必经之路都被堵了,只能来回走等危险期过去),所以标记数组vis需要在玩家碰壁后重新把附近的点置1 void dfs(int x,int y,int t) //优先右下,右下走不了试着左上 { if(x<1 || x>n || y<0 || y>n) return; if(vis[x][y]==0) return; if(x==n && y==m) if(t<mint) mint=t; if(t>=node[x][y].t1 && t<=node[x][y].t2 ) { if(x+1<=n) vis[x+1][y] = 1; if(x-1>=1) vis[x-1][y] = 1; if(y+1<=m) vis[x][y+1] = 1; if(y-1>=1) vis[x][y-1] = 1; } else { vis[x][y]=0; dfs(x+1,y,t+1); dfs(x-1,y,t+1); dfs(x,y+1,t+1); dfs(x,y-1,t+1); } } int main() { //坑点:复制粘贴后 细节未处理清楚 //坑点:危险期的点是可以在危险期之后加入的,角色每个单位时间必须移动 int t; cin>>n>>m>>t; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { vis[i][j]=1; node[i][j].t1 = 100000; node[i][j].t2 = -1; } for(int i=0;i<t;i++) { int r,c,a,b; cin>>r>>c>>a>>b; node[r][c].t1 = a; //a<=b node[r][c].t2 = b; } dfs(1,1,0); cout<<mint; return 0; }
#include<cstdio> #include<iostream> #include<algorithm> #include<vector> #include<string> #include<queue> #include<map> #include<stack> using namespace std; struct Node { int x,y; int nowt; int t1,t2; friend bool operator < (Node a, Node b) { return a.nowt > b.nowt;//结构体中,nowt小的优先级高 } }node[105][105],temp; int n,m; int vis[105][105]; priority_queue<Node> q; //坑点:下一秒的时候坐标(x+1,y)为危险处 不是不可以加入,可以来回走等危险期后加入,根据时间判断+1或+2 bool bfs() { q.push(node[1][1]); vis[1][1] = 0; while(!q.empty()) { temp = q.top(); if(temp.x == n && temp.y == m) //由于危险点的介入,普通最先到达不一定是最快的,但可以用优先队列,保证每次都是用时最短的出队 { cout<<temp.nowt<<endl; return true; } q.pop(); int x = temp.x; int y = temp.y; //不越界,未走过 if(x+1>=1 && x+1<=n && y>=1 &&y<=m && vis[x+1][y]==1 ) { vis[x+1][y] = 0; if(node[x][y].nowt+1 < node[x+1][y].t1 || node[x][y].nowt+1 > node[x+1][y].t2) //bfs保证已经是最快找到该点 { node[x+1][y].nowt = node[x][y].nowt+1; q.push(node[x+1][y]); } else { if((node[x+1][y].t2-node[x+1][y].nowt)%2==0) node[x+1][y].nowt = node[x+1][y].t2+1; else node[x+1][y].nowt = node[x+1][y].t2+2; q.push(node[x+1][y]); } } if(x-1>=1 && x-1<=n && y>=1 &&y<=m && vis[x-1][y]==1) { vis[x-1][y] = 0; if(node[x][y].nowt+1 < node[x-1][y].t1 || node[x][y].nowt+1 > node[x-1][y].t2) //bfs保证已经是最快找到该点 { node[x-1][y].nowt = node[x][y].nowt+1; q.push(node[x-1][y]); } else { if((node[x-1][y].t2-node[x-1][y].nowt)%2==0) node[x-1][y].nowt = node[x-1][y].t2+1; else node[x-1][y].nowt = node[x-1][y].t2+2; q.push(node[x-1][y]); } } if(x>=1 && x<=n && y+1>=1 &&y+1<=m && vis[x][y+1]==1 ) { vis[x][y+1] = 0; if(node[x][y].nowt+1 < node[x][y+1].t1 || node[x][y].nowt+1 > node[x][y+1].t2) //bfs保证已经是最快找到该点 { node[x][y+1].nowt = node[x][y].nowt+1; q.push(node[x][y+1]); } else { if((node[x][y+1].t2-node[x][y+1].nowt)%2==0) node[x][y+1].nowt = node[x][y+1].t2+1; else node[x][y+1].nowt = node[x][y+1].t2+2; q.push(node[x][y+1]); } } if(x>=1 && x<=n && y-1>=1 &&y-1<=m && vis[x][y-1]==1) { vis[x][y-1] = 0; if(node[x][y].nowt+1 < node[x][y-1].t1 || node[x][y].nowt+1 > node[x][y-1].t2) //bfs保证已经是最快找到该点 { node[x][y-1].nowt = node[x][y].nowt+1; q.push(node[x][y-1]); } else { if((node[x][y-1].t2-node[x][y-1].nowt)%2==0) node[x][y-1].nowt = node[x][y-1].t2+1; else node[x][y-1].nowt = node[x][y-1].t2+2; q.push(node[x][y-1]); } } } return false; } int main() { //坑点:复制粘贴后 细节未处理清楚 int t; cin>>n>>m>>t; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { vis[i][j]=1; node[i][j].x = i; node[i][j].y = j; node[i][j].t1 = 100000; node[i][j].t2 = -1; node[i][j].nowt = 0; } for(int i=0;i<t;i++) { int r,c,a,b; cin>>r>>c>>a>>b; node[r][c].t1 = a; //a<=b node[r][c].t2 = b; } bfs(); return 0; }