当前位置: 代码迷 >> 综合 >> AOJ 2132 Left Hand Rule
  详细解决方案

AOJ 2132 Left Hand Rule

热度:62   发布时间:2024-01-12 05:18:39.0

模拟题,给出起点,要求在迷宫中紧贴左侧墙壁走,询问能否走到特定点

读入迷宫时的转化有点麻烦,做这种题重要的是放平心态



/*author: birdstorm*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <complex>
#include <set>
#include <algorithm>
#include <climits>
#include <cfloat>#define MAXN 1005
//#define N 105
#define inf 1.0e20
//#define eps 1.0e-8
#define MOD 1000000007#define pb push_back
#define mp make_pair
#define next(i) (i+1)%sz#define For(i,m,n) for(int i=(m);i<(n);i++)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define rep(i,m,n) for(int i=(m);i<=(n);i++)
#define repd(i,m,n) for(int i=(m);i>=(n);i--)
#define LL long long
#define testusing namespace std;const double eps=1.0e-8;
const double pi=acos(-1.0);
bool wallH[105][105], wallV[105][105];
bool vis[105][105][4];
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
int main()
{//freopen("2132-input.txt","r",stdin);int w, h, n, m, d;int x1, x2, x3, y1, y2, y3;while(~scanf("%d%d%d",&w,&h,&n)&&w){memset(wallH,false,sizeof wallH);memset(wallV,false,sizeof wallV);swap(w,h);rep(i,0,w) wallH[0][i]=wallH[h][i]=true;rep(i,0,h) wallV[i][0]=wallV[i][w]=true;For(i,0,n){scanf("%d%d%d%d",&x1,&y1,&x2,&y2);if(x1==x2){if(y1>y2) swap(y1,y2);For(j,y1,y2){wallH[x1][j]=true;}}else{if(x1>x2) swap(x1,x2);For(j,x1,x2){wallV[j][y1]=true;}}}int x, y, z;scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);x=min(x1,x2);y=min(y1,y2);if(x1==x2){if(!x1) z=2;else z=0, x--;}else{if(!y1) z=1;else z=3, y--;}int cnt=1;bool flag=false;memset(vis,false,sizeof vis);while(true){if(vis[x][y][z]){break;}vis[x][y][z]=true;if(x==x3&&y==y3){flag=true;break;}z=(z+3)%4;For(i,0,4){int nx=x+dx[z], ny=y+dy[z];if(nx>=0&&nx<h&&ny>=0&&ny<w){if(z==0&&!wallH[x][y]||z==1&&!wallV[x][y+1]||z==2&&!wallH[x+1][y]||z==3&&!wallV[x][y]){x=nx, y=ny;break;}}z=(z+1)%4;}cnt++;}if(flag) printf("%d\n",cnt);else printf("Impossible\n");}return 0;
}


  相关解决方案