#include<iostream>
#include<vector>
using namespace std;
class Point
{
public:
int x;
int y;
Point():x(0),y(0)
{
}
Point(int a, int b):x(a),y(b)
{
}
};
/*
-1 sink block
0 empty block
1 start point
2 block of wall
3 door
for vecMap[x][y]
move x -- up or down
move y -- left or right
*/
bool FindWay(Point pStart, vector< vector<int> > vecMap, vector<Point> vecRount,
int iTime, Point pDoor)
{
//从pStart出发,上下左右开始走,加入vecRount
if(pStart.x == pDoor.x && pStart.y == pDoor.y)
{
if(iTime == vecRount.size())
{
cout << "TRUE" << endl;
return true;
}
return false;
}
if(iTime == vecRount.size() )
{
/*
if(3 == vecMap[pStart.x][pStart.y])
{
cout << "TRUE" << endl;
return true;
}
cout << "WRONG" << endl;
for(int i = 0 ; i < vecRount.size(); i++)
{
cout << vecRount[i].x <<"-" << vecRount[i].y << " ";
}
cout << endl;
*/
return false;
}
bool boFindWay = false;
/*
find way
-get new start
-get new rount
-find
*/
if( (0 <= pStart.x - 1)
&& (0 == vecMap[pStart.x - 1][pStart.y] || 3 == vecMap[pStart.x - 1][pStart.y])) // to up
{
//cout << " up " << endl;
Point pNext(pStart.x - 1, pStart.y);
vector<Point> vecNextRount(vecRount);
vecNextRount.push_back(pNext);
//cout << pNext.x << " ___ "<< pNext.y << endl;
vecMap[pNext.x][pNext.y] = -1;
boFindWay = FindWay(pNext, vecMap, vecNextRount, iTime, pDoor);
if(boFindWay == true)
{
return boFindWay;
}
} //up_loop
if((vecMap.size() > pStart.x + 1 )
&& (0 == vecMap[pStart.x + 1][pStart.y] || 3 == vecMap[pStart.x + 1][pStart.y])) // to down
{
//cout << " down " << endl;
Point pNext(pStart.x + 1, pStart.y);
vector<Point> vecNextRount(vecRount);
vecNextRount.push_back(pNext);
//cout << pNext.x << " ___ "<< pNext.y << endl;
vecMap[pNext.x][pNext.y] = -1;
boFindWay = FindWay(pNext, vecMap, vecNextRount, iTime, pDoor);
if(boFindWay == true)
{
return boFindWay;
}
} //down_loop
if( (0 <= pStart.y - 1 )
&& (0 == vecMap[pStart.x][pStart.y - 1] || 3 == vecMap[pStart.x][pStart.y - 1])) // to left
{
//cout << " left " << endl;
Point pNext(pStart.x, pStart.y - 1);
vector<Point> vecNextRount(vecRount);
vecNextRount.push_back(pNext);
//cout << pNext.x << " ___ "<< pNext.y << endl;
vecMap[pNext.x][pNext.y] = -1;
boFindWay = FindWay(pNext, vecMap, vecNextRount, iTime, pDoor);
if(boFindWay == true)
{
return boFindWay;
}
} //left_loop
if( (vecMap[0].size() > pStart.y + 1)
&& (0 == vecMap[pStart.x][pStart.y + 1] || 3 == vecMap[pStart.x][pStart.y + 1])) // to right
{
//cout << " right " << endl;
Point pNext(pStart.x, pStart.y + 1);
vector<Point> vecNextRount(vecRount);
vecNextRount.push_back(pNext);
//cout << pNext.x << " ___ "<< pNext.y << endl;
vecMap[pNext.x][pNext.y] = -1;
boFindWay = FindWay(pNext, vecMap, vecNextRount, iTime, pDoor);
if(boFindWay == true)
{
return boFindWay;
}
} //right_loop
/*
for(int i = 0 ; i < vecRount.size(); i++)
{
cout << vecRount[i].x <<"-" << vecRount[i].y << " ";
}
cout << endl;
*/
return false;
}
int main()
{
int iLine, iRow;
int iTime;
while(cin >> iLine >> iRow >> iTime)
{
if(0 == iLine && 0 == iRow && 0 == iTime)
{
return 0;
}
else
{
char ch;
vector< vector<int> > vecMap(iLine);
for(int i = 0; i < iLine; i++)
{
vector<int> vecRow(iRow);
vecMap[i] = vecRow;
}
Point pStart, pDoor;
for(int i = 0; i < iLine; i++)
{
for(int j = 0; j < iRow; j++)
{
cin >> ch;
if('S' == ch)
{
vecMap[i][j] = 1;
pStart.x = i;
pStart.y = j;
}
else if('.' == ch)
{
vecMap[i][j] = 0;
}
else if('X' == ch)
{
vecMap[i][j] = 2;
}
else if('D' == ch)
{
vecMap[i][j] = 3;
pDoor.x = i;
pDoor.y = j;
}
}//for_loop
}//for_loop
for(int i = 0; i < iLine; i++)
{
for(int j = 0; j < iRow; j++)
{
cout << vecMap[i][j] << " ";
}//for_loop
cout << endl;
}//for_loop
vector<Point> vecRount;
if(FindWay(pStart, vecMap, vecRount,iTime , pDoor) )
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl;
}
}//if_loop
}//while_loop
return 0;
}
题目:点击打开链接
基本做法:让它随意游走,到时间在看是否走到门口,走到死胡同就回溯。不过,这样笨方法超时了。
参考答案:点击打开链接 (高手写的简单明了,技不如人啊)