# CleaningRobot

ptrdung
plain_text
2 months ago
4.8 kB
1
Indexable
Never
```We have to plan a path for a cleaning robot to clean a rectangular room floor of size NxM. The room floor paved with square tiles whose size fits the cleaning robot (1 × 1). There are clean tiles and dirty tiles, and the robot can change a dirty tile to a clean tile by visiting the tile. Also there may be some obstacles (furniture) whose size fits a tile in the room. If there is an obstacle on a tile, the robot cannot visit it. The robot moves to an adjacent tile with one move. The tile onto which the robot moves must be one of four tiles (i.e., east, west, north or south) adjacent to the tile where the robot is present. The robot may visit a tile twice or more.

Your task is to write a program which computes the minimum number of moves for the robot to change all dirty tiles to clean tiles, if ever possible.

Time limit: 1s (C/C++), 2s (Java)

Submit limit: 10 times
Input

The input consists of multiple maps, the first line is the number of test case T (T < = 50).

Each test case begins with N and M representing the size of the room. ( 5 =< N, M <= 100)

The next N line representing the arrangement of the room with following describe:

0 : a clean tile
1 : a dirty tile
2 : a piece of furniture (obstacle)
3 : the robot (initial position)

In the map the number of dirty tiles does not exceed 10 and there is only one robot.

Output

Print each test case on two lines, the first line of each test case is "Case #x", where x is the test case number. The next line is the minimum number of moves for the robot to change all dirty tiles to clean tiles. If the map includes dirty tiles which the robot cannot reach, your program should output -1.

Sample

Input

5

5 7

0 0 0 0 0 0 0

0 3 0 0 0 1 0

0 0 0 0 0 0 0

0 1 0 0 0 1 0

0 0 0 0 0 0 0

5 15

0 0 0 0 2 0 2 0 0 0 0 1 2 0 1

0 0 0 1 0 2 0 2 2 0 1 2 0 0 0

2 1 0 2 0 1 0 2 0 0 0 0 0 0 0

0 0 0 1 0 2 0 0 1 2 0 0 2 0 0

0 2 1 0 2 0 0 0 0 0 3 0 0 0 0

...............

Output

Case #1

8

Case #2

38

Case #3

37

Case #4

-1

Case #5

49

#include <iostream>

using namespace std;

int N, M;
int mattrix[100][100]; // san nha
int markDirty[100][100]; // danh dau san nha ban
int matranke[100][100]; // khoang cach cac o ban
int visited[100][100];
int xQueue[100000], yQueue[100000], first, last, xCurrent, yCurrent, xNext, yNext;
int idDirty;
int numDirty;
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int ans;
int visitedBT[100];

void BFS(int x, int y)
{
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
visited[i][j] = 0;
}
}
first = 0;
last = 0;
xQueue[last] = x;
yQueue[last++] = y;
visited[x][y] = 0;

while(first != last)
{
xCurrent = xQueue[first];
yCurrent = yQueue[first++];
for(int i = 0; i < 4; i++)
{
xNext = xCurrent + dx[i];
yNext = yCurrent + dy[i];
if(xNext >= 0 && xNext < N && yNext >= 0 && yNext < M)
{
if(mattrix[xNext][yNext] != 2 && visited[xNext][yNext] == 0)
{
visited[xNext][yNext] = visited[xCurrent][yCurrent] + 1;
xQueue[last] = xNext;
yQueue[last++] = yNext;
if(markDirty[xNext][yNext] > 0)
{
matranke[markDirty[x][y]][markDirty[xNext][yNext]] = visited[xNext][yNext];
}
}
}
}
}
}

void BT(int n, int a, int sum)
{
if (sum > ans){
return;
}
if(n == idDirty - 1)
{
if(ans > sum) ans = sum;
}
else
{
for (int i = 2; i < idDirty; i++)
{
if(visitedBT[i] == 0)
{
visitedBT[i] = 1;
BT(n+1, i, sum + matranke[a][i]);
visitedBT[i] = 0;
}
}
}
}

int main()
{
freopen("input.txt", "r", stdin);
int T;
cin >> T;
for(int tc = 1; tc <= T; tc++)
{
ans = 999999;
cin >> N >> M;
idDirty = 2;
for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{

markDirty[i][j] = 0;
visited[i][j] = 0;

matranke[i][j] = 0;
cin >> mattrix[i][j];
if (mattrix[i][j] == 3) markDirty[i][j] = 1;
if (mattrix[i][j] == 1) markDirty[i][j] = idDirty++;
}
}
numDirty = idDirty - 2; // so o vuong bi ban

for(int i = 0; i < N; i++)
{
for(int j = 0; j < M; j++)
{
if(markDirty[i][j] != 0)
{
//reset visited
BFS(i,j);
}
}
}
//for (int i=1; i<idDirty; i++)
//{
//	for(int j =1; j < idDirty; j++)
//		cout << matranke[i][j] << " ";
//	cout << endl;
//}

for(int i = 2; i < idDirty; i++)
{
if(matranke[1][i] == 0)
{
ans = -1;
break;
}
}
for (int i =0; i <100; i++) visitedBT[i] = 0;
if(ans == -1) cout << "Case #" << tc << endl << ans << endl;
else
{
BT(1,1,0);
cout << "Case #" << tc << endl << ans << endl;
}
}
return 0;
}

```