首页 > ACM题库 > HDU-杭电 > HDU 3912-Turn Right[解题报告]HOJ
2015
04-14

HDU 3912-Turn Right[解题报告]HOJ

Turn Right

问题描述 :

This summer, ELT and his classmates went to Beijing for a training of coding. ELT have never been to Beijing before, so at the weekend, he together with some friends went to the National Museum, it’s free for students!

  The National Museum consists of many parts. One part of it is an exhibition of Ancient China From Xia Dynasty to Qing Dynasty, it needs a big room to show all the things. What’s more, there exist many walls to hang pictures. The boundary of this room is walls except the entrance and exit.

  With walls, an entrance and an exit, this room can be regarded as a maze. To make it simple, this room is a R*C grid, wall is constructed at some edges of grid. The entrance is always at the first row, and the exit is always at the last row, just like the picture below.

Black And White

ELT can’t remember his direction in maze, but he is a clever boy. He knew an algorithm called "Always Turn Right", it’s procedure is as follows: at any grid of this room, if we can turn right(no wall at right side), then we must turn right; if we can’t turn right but can go straight forward, then we must go forward; if we can’t go forward but can turn left, then we must turn left; if we can’t even turn left, we just turn backward. In the picture above, if we use this algorithm, we’ll visit these grids in order: Entrance –> (0, 1) –> (0, 0) –> (0, 1) –> (0, 2) –> (1, 2) –> (1, 1) –> (1, 0) –> (2, 0) –> (1, 0) –> (1, 1) –> (2, 1) –> (2, 2) –> Exit. Very easy, doesn’t it?

  ELT uses "Always Turn Right" algorithm to visit this room from entrance to exit, and then from exit to entrance. He wants to know whether he walked all grids in the room. Now ELT is dizzy because the maze is too big, can you help him?

输入:

First line is an integer T, means T test cases. In each test case, the first line has four numbers: R, C, Ent_Column, Exit_Column. Ent_Column is the column number of entrance; Exit_Column is the column number of exit.
Then following 2*R-1 lines, 2*i line have C-1 numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i, j+1), 2*i+1 line have C numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i+1, j). Number 1 represents a wall, 0 represents no wall.
  We guarantee that there exists a path from entrance to exit.
2 <= R, C <= 500
0 <= Ent_Column, Exit_Column < C

输出:

First line is an integer T, means T test cases. In each test case, the first line has four numbers: R, C, Ent_Column, Exit_Column. Ent_Column is the column number of entrance; Exit_Column is the column number of exit.
Then following 2*R-1 lines, 2*i line have C-1 numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i, j+1), 2*i+1 line have C numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i+1, j). Number 1 represents a wall, 0 represents no wall.
  We guarantee that there exists a path from entrance to exit.
2 <= R, C <= 500
0 <= Ent_Column, Exit_Column < C

样例输入:

1
3 4 1 2
0 0 0
1 1 0 1
0 0 0
0 0 0 0
1 0 0

样例输出:

YES

#include <cstdio>
#include <cstring>
#include <cstdlib>

const int Max=501;
int R,C,E,X;
//r: (i,j) -> (i,j+1)
//c: (i,j) -> (i+1,j)
int r[Max][Max],c[Max][Max];
int dx[]={-1, 0, 1, 0};
int dy[]={ 0, 1, 0,-1};
bool go(int i,int j,int d){
	switch(d){
		case 0: return i>0 && !c[i-1][j];
		case 1: return j+1<C && !r[i][j];
		case 2: return i+1<R && !c[i][j];
		case 3: return j>0 && !r[i][j-1];
	}
	return false;
}
bool vst[Max][Max];
int main(){
	int TT;
	scanf("%d",&TT);
	for(int cas=1;cas<=TT;++cas){
		scanf("%d %d %d %d",&R,&C,&E,&X);
		for(int i=0;i<R;++i){
			memset(vst[i],0,sizeof(vst[i][0])*C);
			for(int j=0;j+1<C;++j) scanf("%d",r[i]+j);
			if(i+1>=R) continue;
			for(int j=0;j<C;++j) scanf("%d",c[i]+j);
		}
		int x=0,y=E,d=2,cnt=R*C;
		vst[x][y]=true,--cnt;
		for(bool con=true;con;){
			//printf("%d %d %d\n",x,y,d);system("pause");
			d=(d+2)&3;
			for(int i=0;i<4;++i){
				d=(d+3)&3;
				if(x==R-1 && y== X && d==2){
					con=false;	
					break;
				}
				if(!go(x,y,d)) continue;
				x+=dx[d],y+=dy[d];
				if(!vst[x][y]) vst[x][y]=true,--cnt;
				break;
			}
		}
		if(cnt>0){
			x=R-1,y=X,d=0;
			for(bool con=true;con;){
				//printf("%d %d %d\n",x,y,d);system("pause");
				d=(d+2)&3;
				for(int i=0;i<4;++i){
					d=(d+3)&3;
					if(x==0 && y==E && d==0){
						con=false;
						break;
					}
					if(!go(x,y,d)) continue;
					x+=dx[d],y+=dy[d];
					if(!vst[x][y]) vst[x][y]=true,--cnt;
					break;
				}
			}
		}
		puts(cnt==0?"YES":"NO");
		/*
		puts("done");
		for(int i=0;i<R;++i){
			for(int j=0;j+1<C;++j) printf("%d ",r[i][j]);
			puts("");
		}
		for(int i=0;i+1<R;++i){
			for(int j=0;j<C;++j) printf("%d ",c[i][j]);
			puts("");
		}
		*/
	}
	return 0;
}

  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }