| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
WA的不妨再看看题目规则之前漏了“出地图会失败”,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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator