Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

BOJ 1461

$
0
0

bfs搜索

转动平面相当于改变重力的方向

#include <cstdio>
#include <cstring>
#include <queue>
#define N 110
using namespace std;

struct line{
	int x0,y0,x1,y1;
	bool flag; //flag=1从左到右
}lines[N];

struct Point{
	int x,y,dir;
};

bool vis[N][N][4];
int dp[N][N][4]; //0表示重力向下 1表示重力向左...
int x0,y0,x1,y1;
int w,h,n;
int dir[4][2]={0,-1,-1,0,0,1,1,0};    //二位数组按行存储(WA死了)

bool check(int x,int y){
	if(x>=0 && x<w && y>=0 && y<h)return 1;
	return 0;
}

int bfs(){
	queue<Point>q;
	Point p1,p2;
	int i,j;
	memset(dp,-1,sizeof(dp));
	dp[x0][y0][0]=0;
	p1.x=x0,p1.y=y0,p1.dir=0,q.push(p1);
	while(!q.empty()){
		p1=q.front();
		q.pop();
		if(p1.x==x1 && p1.y==y1) return dp[x1][y1][p1.dir];
		int nowdir=(p1.dir+1)%4,x=p1.x,y=p1.y;
		while(check(x+dir[nowdir][0],y+dir[nowdir][1]) && !(x==x1 && y==y1) && !vis[x][y][nowdir]) x+=dir[nowdir][0],y+=dir[nowdir][1];
		if(dp[x][y][nowdir]==-1){
			dp[x][y][nowdir]=dp[p1.x][p1.y][p1.dir]+1;
			p2.x=x,p2.y=y,p2.dir=nowdir;
			q.push(p2);
		}

		nowdir=(p1.dir+3)%4,x=p1.x,y=p1.y;
                while(check(x+dir[nowdir][0],y+dir[nowdir][1]) && !(x==x1 && y==y1) && !vis[x][y][nowdir]) x+=dir[nowdir][0],y+=dir[nowdir][1];
		if(dp[x][y][nowdir]==-1){
                        dp[x][y][nowdir]=dp[p1.x][p1.y][p1.dir]+1;
                        p2.x=x,p2.y=y,p2.dir=nowdir;
                        q.push(p2);
                }

	}
	return -1;
}

int main(){
	int t,T;
	int i,j;
	scanf("%d",&T);
	for(t=1;t<=T;t++){
		scanf("%d %d %d",&w,&h,&n);
		scanf("%d %d %d %d",&x0,&y0,&x1,&y1);
		memset(vis,0,sizeof(vis));
		for(i=1;i<=n;i++){
			scanf("%d %d %d %d",&lines[i].x0,&lines[i].y0,&lines[i].x1,&lines[i].y1);
			if(lines[i].x0==lines[i].x1){  // ==老写错
				for(j=lines[i].y0;j<lines[i].y1;j++) vis[lines[i].x0-1][j][3]=1,vis[lines[i].x0][j][1]=1;
			}
			else{
				for(j=lines[i].x0;j<lines[i].x1;j++) vis[j][lines[i].y0-1][2]=1,vis[j][lines[i].y0][0]=1;
			}
		}
		printf("%d\n",bfs());  
	} 
}


作者:waitfor_ 发表于2013-3-25 23:41:48 原文链接
阅读:105 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>