http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119
1119: Collecting Coins
Time Limit: 3 Sec Memory Limit: 128 MB
Submit: 144 Solved: 35
[Submit][Status][Web Board]
Description
In a maze of r rows and c columns, your task is to collect as many coins as possible.
Each square is either your start point "S"(which will become empty after you leave), an empty square ".", a coin square "C" (which will become empty after you step on this square and thus collecting the coin), a rock square "O" or an obstacle square "X".
At each step, you can move one square to the up, down, left or right. You cannot leave the maze or enter an obstacle square, but you can push each rock at most once (i.e. You can treat a rock as an obstacle square after you push it).
To push a rock, you must stand next to it. You can only push the rock along the direction you're facing, into an neighboring empty square (you can't push it outside the maze, and you can't push it to a squarecontiaining a coin).For example, if the rock is to your immediate right, you can only push it to its right neighboring square.
Find the maximal number of coins you can collect.
Input
The first line of input contains a single integer T (T<=25), the number of test cases.
Each test case begins with two integers r and c (2<=r,c<=10), then followed by r lines, each with c columns.
There will be at most 5 rocks and at most 10 coins in each maze.
Output
For each test case, print the maximal number of coins you can collect.
Sample Input
3
3 4
S.OC
..O.
.XCX
4 6
S.X.CC
..XOCC
...O.C
....XC
4 4
.SXC
OO.C
..XX
.CCC
Sample Output
1
6
3
HINT
Source
分析;
BFS
AC代码;
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL long long using namespace std;
int mp[][];
int vis[][];
int xadd[] = {,-,,};
int yadd[] = {,,,-};
struct node
{
int x;int y;
int is;
node(int _x, int _y, int _is)
{
x = _x;
y = _y;
is = ;
}
};
vector<node> C;
int mx = ;
int ansnum = ;
int n , m ;
void debug()
{
for(int i = ;i <= n;i ++)
{
for(int j = ;j <= m;j ++)
printf("%d ",mp[i][j]);
printf("\n");
}
}
void bfs(int x, int y ,int ans)
{
//printf("%d %d\n",x,y);
if(mx == ansnum)
return;
vector<node> Q;
Q.push_back(node(x,y,));
vis[x][y] = ;
int l = ;
int r = ;
while(l <= r )
{
for(int i = ;i <= ;i ++)
{
int tx = Q[l].x + xadd[i] ;
int ty = Q[l].y + yadd[i] ;
if(mp[tx][ty] >= && !vis[tx][ty])
{
vis[tx][ty] = ;
r ++ ;
if(mp[tx][ty] == )
{
Q.push_back(node(tx,ty,));
ans ++ ;
}
else Q.push_back(node(tx,ty,));
}
}
l ++ ;
}
if(ans > mx)
mx = ans;
for(int i = ;i < C.size();i ++)
{
if(!C[i].is)
{
for(int s = ;s <= ;s ++)
{
int tx = C[i].x + xadd[s];
int ty = C[i].y + yadd[s];
int ttx = C[i].x - xadd[s];
int tty = C[i].y - yadd[s];
//printf("%d %d %d %d\n",tx,ty,ttx,tty);
if(mp[tx][ty] == && vis[ttx][tty] == )
{
mp[tx][ty] = -;
mp[C[i].x][C[i].y] = ;
C[i].is = ;
bfs(C[i].x,C[i].y,ans);
mp[tx][ty] = ;
mp[C[i].x][C[i].y] = ;
C[i].is = ;
}
}
}
}
for(int i = r; i >= ;i --)
{
vis[Q[i].x][Q[i].y] = ;
if(Q[i].is)
{
mp[Q[i].x][Q[i].y] = ;
}
}
}
int main(){
int t ;
scanf("%d",&t);
while(t--)
{
memset(mp,-,sizeof(mp));
memset(vis,,sizeof(vis));
scanf("%d %d",&n,&m);
char str[];
int bex, bey ;
ansnum = ;
C.clear();
for(int i = ;i <= n;i ++)
{
scanf("%s",&str[]);
for(int j = ;j <= m; j ++)
{
if(str[j] == 'S')
{
mp[i][j] = ;
bex = i ;
bey = j ;
}else if(str[j] == 'C')
{
ansnum ++;
mp[i][j] = ;
}else if(str[j] == 'X')
{
mp[i][j] = -;
}else if (str[j] == 'O'){
mp[i][j] = ;
C.push_back(node(i,j,));
}else {
mp[i][j] = ;
}
}
}
mx = ;
bfs(bex,bey,);
printf("%d\n",mx);
} return ;
}