Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

史上最艰难的AC,好不容易终于过了!撒花贴代码留念!

Posted by nan5515522 at 2014-05-03 12:44:24 on Problem 3083
刚开始沿着墙走的意思没理解,后来模拟各种不顺利。
调这破程序跳的快不会爱了,终于AC了,好不容易啊!

#include<iostream>
#include<Cstdio>
using namespace std;

int movX[4] = {-1, 0, 1, 0};
int movY[4] = {0, 1, 0, -1};
int map[101][101], wide, height, endX, endY, startX, startY;
char input[100];

///////////////

struct node
{
	int x, y, time;
};

int hash[101][101];
node q[2000];

/////////////////


bool check1(int x, int y, int dir)
{
	
	bool flag1 = (x >= 0) && (x < height) 
	&& (y >= 0) && (y < wide) && (map[x][y] != -1);

		
	int d1 = (dir + 1) % 4;
	int x1 = x + movX[d1], y1 = y + movY[d1];
	
	bool flag2 = (x1 >= 0) && (x1 < height) 
	&& (y1 >= 0) && (y1 < wide) && (map[x1][y1] == -1 || map[x1][y1] == 1);
	
//	cout << x << " " << y <<endl;
//	cout << x1 << " " << y1 <<endl;
//	cout << flag1 << " "
//	cout << endl;
	return (flag1 && flag2);

}


int dfs1(int tempX, int tempY, int direction)
{
	//cout << tempX << " " << tempY <<endl;
	if(tempX == endX && tempY == endY)
	{
		return 1;
	}
	
	int d[4] = {(direction + 1)%4, direction, (direction + 7) %4
				, (direction + 2)%4};
	int posX[4], posY[4];
	
	posX[0] = tempX + movX[d[0]]; posY[0] = tempY + movY[d[0]];
	posX[1] = tempX + movX[d[1]]; posY[1] = tempY + movY[d[1]];
	posX[2] = tempX + movX[d[2]]; posY[2] = tempY + movY[d[2]];
	posX[3] = tempX + movX[d[3]]; posY[3] = tempY + movY[d[3]];
	
	if(check1(posX[0], posY[0], d[0]))
	     return dfs1(posX[0], posY[0], d[0]) + 1;
	
	if(check1(tempX, tempY, direction) && (map[posX[1]][posY[1]] != -1))
		return dfs1(posX[1], posY[1], d[1]) + 1;
	
	
	if(check1(posX[2], posY[2], d[2]))
	   return dfs1(posX[2], posY[2], d[2]) + 1;
	

	return dfs1(posX[3], posY[3], d[3]) + 1;
}

bool check2(int x, int y, int dir)
{
	
	bool flag1 = (x >= 0) && (x < height) 
	&& (y >= 0) && (y < wide) && (map[x][y] != -1);
	
	
	int d1 = (dir + 3) % 4;
	int x1 = x + movX[d1], y1 = y + movY[d1];
	
	bool flag2 = (x1 >= 0) && (x1 < height) 
	&& (y1 >= 0) && (y1 < wide) && (map[x1][y1] == -1 || map[x1][y1] == 1);
	
	//	cout << x << " " << y <<endl;
	//	cout << x1 << " " << y1 <<endl;
	//	cout << flag1 << " "
	//	cout << endl;
	return (flag1 && flag2);
	
}


int dfs2(int tempX, int tempY, int direction)
{
	//cout << tempX << " " << tempY <<endl;
	if(tempX == endX && tempY == endY)
	{
		return 1;
	}
	
	int d[4] = {(direction + 3)%4, direction, (direction + 1) %4
		, (direction + 2)%4};
	int posX[4], posY[4];
	
	posX[0] = tempX + movX[d[0]]; posY[0] = tempY + movY[d[0]];
	posX[1] = tempX + movX[d[1]]; posY[1] = tempY + movY[d[1]];
	posX[2] = tempX + movX[d[2]]; posY[2] = tempY + movY[d[2]];
	posX[3] = tempX + movX[d[3]]; posY[3] = tempY + movY[d[3]];
	
	if(check2(posX[0], posY[0], d[0]))
		return dfs2(posX[0], posY[0], d[0]) + 1;
	
//	cout << (map[posX[1]][posY[1]]!= -1)<< endl;
//	cout << check2(tempX, tempY, direction) << endl;

	if(check2(tempX, tempY, direction) && (map[posX[1]][posY[1]] != -1))
		return dfs2(posX[1], posY[1], d[1]) + 1;
	
	
	if(check2(posX[2], posY[2], d[2]))
		return dfs2(posX[2], posY[2], d[2]) + 1;
	
	
	return dfs2(posX[3], posY[3], d[3]) + 1;
}
int bfs()
{
	int front = 0, end = 1, x, y, time;
	q[0].x = startX; q[0].y = startY; q[0].time = 0;
	while (end > front)
	{
		x = q[front].x; y = q[front].y; time = q[front].time;
		//cout << x << " " << y << endl;
		front++;
	
		if(x + 1 < height && map[x+1][y] != -1&& hash[x+1][y] == 0)
		{
			if(x + 1 == endX && y == endY)
				return time + 2;
			q[end].x = x + 1; q[end].y = y; q[end].time = time + 1;
			hash[x+1][y] = 1;
			end ++;
		}
		
		if(x - 1 >= 0 && map[x-1][y] != -1 && hash[x-1][y] == 0)
		{
			if(x - 1 == endX && y == endY)
				return time + 2;
			q[end].x = x - 1; q[end].y = y; q[end].time = time + 1;
			hash[x-1][y] = 1;
			end ++;
		}
		
		
		if(y + 1 < wide && map[x][y + 1] != -1 && hash[x][y+1] == 0)
		{
			if(x == endX && y + 1 == endY)
				return time + 2;
			q[end].x = x; q[end].y = y + 1; q[end].time = time + 1;
			hash[x][y+1] = 1;
			end ++;
		}
		
		if(y - 1 >= 0 && map[x][y - 1] != -1 &&hash[x][y-1] == 0)
		{
			if(x == endX && y - 1 == endY)
				return time + 2;
			q[end].x = x; q[end].y = y - 1; q[end].time = time + 1;
			hash[x][y-1] = 1;
			end ++;
		}
	}
	
	return -1;
}

int main()
{
	int size, i, j, dir;
	char temp;
	cin >> size;
	while(size--)
		
	{

		scanf("%d%d", &wide, &height);
		
		
		for(i = 0; i < 41; i++)
		{
			for(j = 0; j < 41; j++)
			{
				hash[i][j] = 0;
			}
		}
		for(i = 0; i < height; i ++)
		{
			scanf("%s", input);
			for(j = 0; j < wide; j++)
			{

				temp = input[j];
				if(temp == '.')
					map[i][j] = 0;
				else if(temp == '#')
					map[i][j] = -1;
				else if(temp == 'S')
				{
					startX = i; startY = j;
					map[i][j] = 1;
				}
				else if(temp == 'E')
				{
					endX = i; endY = j;
					map[i][j] = 2;
				}
			}
		}	
		

		if(startY == 0)
			dir = 1;
		if(startY == wide - 1)
			dir = 3;
		if(startX == 0)
			dir = 2;
		if(startX == height - 1)
			dir = 0;
		
		cout << dfs2(startX, startY, dir) << " ";
		cout << dfs1(startX, startY, dir) << " ";
     	cout << bfs() << endl;
		
//		cout << bfs() << endl;
//		for(i = 0; i < height; i ++)
//		{
//			for(j = 0; j < wide; j++)
//			{
//				printf("%d ", map[i][j]);
//
//			}
//			cout << endl;
//		}
	}
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator