| ||||||||||
| 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,怎么会错呢,试了n多数据都没问题啊,求帮助!#include <iostream>
using namespace std;
char maze[25][49],str[5];
int visited[13][13];
int n,m,begin_x,end_x,begin_y,end_y;
int step;
void Init()
{
int i,j;
step = 1;
memset(maze,0,sizeof(maze));
memset(visited,0,sizeof(visited));
memset(str,0,sizeof(str));
for(i = 0;i < 49;i++)//第一行
if(i % 4 == 0)
maze[0][i] = '+';
else
maze[0][i] = '-';
for(i = 1;i < 24;i++) {//中间
for(j = 4;j < 49;j += 4) {
if(i % 2 != 0)
maze[i][j] = 0;
else
maze[i][j] = '+';
}
if(i % 2 != 0)//最后一列
maze[i][48] = maze[i][0] = '|';
else
maze[i][48] = maze[i][0] = '+';
}
memcpy(maze[24],maze[0],49);
}
bool go_west(int x,int y)//西
{
if(maze[x * 2 - 1][y * 4 - 4] == '|' || maze[x * 2 - 1][y * 4 - 3] == '+' || visited[x][y - 1] == 1)
return false;
else
return true;
}
bool go_north(int x,int y)//北
{
if(maze[x * 2 - 2][y * 4 - 1] == '-' || maze[x * 2 - 2][y * 4 - 1] == '+' || visited[x - 1][y] == 1)
return false;
else
return true;
}
bool go_east(int x,int y)//东
{
if(maze[x * 2 - 1][y * 4] == '|' || maze[x * 2 - 1][y * 4] == '+' || visited[x][y + 1] == 1)
return false;
else
return true;
}
bool go_south(int x,int y)//南
{
if(maze[x * 2][y * 4 - 1] == '-' || maze[x * 2][y * 4 - 1] == '+' || visited[x + 1][y] == 1)
return false;
else
return true;
}
bool findPath(int x,int y)
{
//cout << x << " " << y << endl;
//itoa(step++,str,10);
int i = 0,tmp = step++;
while(tmp != 0){
str[i++] = tmp % 10 + '0';
tmp /= 10;
}
if(strlen(str) == 1)
maze[x * 2 - 1][y * 4 - 1] = str[0];
else if(strlen(str) == 2) {
maze[x * 2 - 1][y * 4 - 2] = str[1];
maze[x * 2 - 1][y * 4 - 1] = str[0];
}
else {
maze[x * 2 - 1][y * 4 - 3] = str[2];
maze[x * 2 - 1][y * 4 - 2] = str[1];
maze[x * 2 - 1][y * 4 - 1] = str[0];
}
visited[x][y] = 1;
if(x == end_x && y == end_y)//递归出口
return true;
if(go_west(x,y) == true && findPath(x,y - 1) == true)
return true;
if(go_north(x,y) == true && findPath(x - 1,y) == true)
return true;
if(go_east(x,y) == true && findPath(x,y + 1) == true)
return true;
if(go_south(x,y) == true && findPath(x + 1,y) == true)
return true;
else {
maze[x * 2 - 1][y * 4 - 3] = '?';
maze[x * 2 - 1][y * 4 - 2] = '?';
maze[x * 2 - 1][y * 4 - 1] = '?';
step--;
return false;
}
}
int main()
{
int i,j,k = 1;
cin >> n >> m >> begin_x >> begin_y >> end_x >> end_y;
while(n != 0 && m != 0 && begin_x != 0 && begin_y != 0 && end_x != 0 && end_y != 0) {
Init();
//for(i = 0;i < 25;i++) {//测试
// for(j = 0;j < 49;j++)
// cout << maze[i][j];
// cout << endl;
//}
int value;
for(i = 1;i <= n;i++)
for(j = 1;j <= m;j++) {
cin >> value;
if(value == 0) {}//没有东墙,没有南墙
else if(value == 1)//只有东墙
maze[i * 2 - 1][j * 4] = '|';
else if(value == 2) {//只有南墙
maze[i * 2][j * 4 - 3] = '-';
maze[i * 2][j * 4 - 2] = '-';
maze[i * 2][j * 4 - 1] = '-';
}
else {//东墙,南墙都有
maze[i * 2 - 1][j * 4] = '|';
maze[i * 2][j * 4 - 3] = '-';
maze[i * 2][j * 4 - 2] = '-';
maze[i * 2][j * 4 - 1] = '-';
}
}
memcpy(maze[n * 2],maze[24],49);//复制一行
for(i = 0;i < 15;i++)//复制一列
maze[i][m * 4] = maze[i][48];
for(i = 0;i <= n * 2;i++) {//测试
for(j = 0;j <= m * 4;j++)
cout << maze[i][j];
cout << endl;
}
findPath(begin_x,begin_y);
cout << "Maze " << k++ << endl;
cout << endl;
for(i = 0;i <= n * 2;i++) {//输出结果
for(j = 0;j <= m * 4;j++)
cout << maze[i][j];
cout << endl;
}
cout << endl;
cin >> n >> m >> begin_x >> begin_y >> end_x >> end_y;
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator