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

WA的不妨再看看题目规则

Posted by Emilia at 2017-03-05 10:36:55 on Problem 3009
之前漏了“出地图会失败”,WA了半天
#include<iostream>
#include<algorithm>

using namespace std;

int steps(int xbeg, int ybeg, int flag);

int oriMap[22][22];

int main()
{
	int width, height;
loop:while (1)
{
	cin >> width >> height;
	if (!width)
		return 0;
	memset(oriMap, -1, sizeof(oriMap));
	for (int i = 1; i <= height; i++)
		for (int t = 1; t <= width; t++)
		{
			cin >> oriMap[i][t];
		}
	for (int i = 1; i <= height; i++)
		for (int t = 1; t <= width; t++)
		{
			if (oriMap[i][t] == 2)
			{
				t = steps(i, t, 1);
				if (t < 11)
					cout << t << endl;
				else
					cout << -1 << endl;
				goto loop;
			}
		}

}
}

int steps(int xbeg, int ybeg, int flag)
{
	if (flag > 10)
		return 11;
	int res[4] = { 11,11,11,11 }, i = 1;
	if (oriMap[xbeg - 1][ybeg] != 1 && oriMap[xbeg - 1][ybeg] != -1)
	{
		i = 1;
		while (oriMap[xbeg - i][ybeg] != 1 && oriMap[xbeg - i][ybeg] != -1 && oriMap[xbeg - i][ybeg] != 3)
			i++;
		switch (oriMap[xbeg - i][ybeg])
		{
		case 1:
			oriMap[xbeg - i][ybeg] = 0;
			res[0] = steps(xbeg - i + 1, ybeg, flag + 1) + 1;
			oriMap[xbeg - i][ybeg] = 1;
			break;
		case -1:
			res[0] = 11;
			break;
		case 3:
			return 1;
			break;
		}

	}
	if (oriMap[xbeg][ybeg + 1] != 1 && oriMap[xbeg][ybeg + 1] != -1)
	{
		i = 1;
		while (oriMap[xbeg][ybeg + i] != 1 && oriMap[xbeg][ybeg + i] != -1 && oriMap[xbeg][ybeg + i] != 3)
			i++;
		switch (oriMap[xbeg][ybeg + i])
		{
		case 1:
			oriMap[xbeg][ybeg + i] = 0;
			res[1] = steps(xbeg, ybeg + i - 1, flag + 1) + 1;
			oriMap[xbeg][ybeg + i] = 1;
			break;
		case -1:
			res[1] = 11;
			break;
		case 3:
			return 1;
			break;
		}
	}
	if (oriMap[xbeg + 1][ybeg] != 1 && oriMap[xbeg + 1][ybeg] != -1)
	{
		i = 1;
		while (oriMap[xbeg + i][ybeg] != 1 && oriMap[xbeg + i][ybeg] != -1 && oriMap[xbeg + i][ybeg] != 3)
			i++;
		switch (oriMap[xbeg + i][ybeg])
		{
		case 1:
			oriMap[xbeg + i][ybeg] = 0;
			res[2] = steps(xbeg + i - 1, ybeg, flag + 1) + 1;
			oriMap[xbeg + i][ybeg] = 1;
			break;
		case -1:
			res[2] = 11;
			break;
		case 3:
			return 1;
			break;
		}
	}
	if (oriMap[xbeg][ybeg - 1] != 1 && oriMap[xbeg][ybeg - 1] != -1)
	{
		i = 1;
		while (oriMap[xbeg][ybeg - i] != 1 && oriMap[xbeg][ybeg - i] != -1 && oriMap[xbeg][ybeg - i] != 3)
			i++;
		switch (oriMap[xbeg][ybeg - i])
		{
		case 1:
			oriMap[xbeg][ybeg - i] = 0;
			res[3] = steps(xbeg, ybeg - i + 1, flag + 1) + 1;
			oriMap[xbeg][ybeg - i] = 1;
			break;
		case -1:
			res[3] = 11;
			break;
		case 3:
			return 1;
			break;
		}
	}
	sort(res, res + 4);
	return res[0];
}



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