第八届湖南省赛I题 Collecting Coins(BFS)

1119: Collecting Coins

Time Limit: 3 Sec   Memory Limit: 128 MB
Submit: 98   Solved: 25
[ 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

题意:1个最大为10*10的格子,从S出发,求寻找硬币C的最大个数。'.'代表可以走的位置,X代表障碍不可走的位置,O代表石头位置,可以往前面方向推一格,当然要保证石头后面是'.',每个格子可以走无数次。石头最多只有5个。

我们可以先把能走的位置走完,并标记出来。然后再枚举怎么移动石头。记得回朔保存状态就行。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
#include <ctime>
#define LL __int64
#define eps 1e-8
using namespace std;
int xx[4]={0,0,-1,1};
int yy[4]={-1,1,0,0};
struct node
{
	int x,y;
	node (){};
	node (int x,int y):x(x),y(y){};
};
node st[10];
int T,ans,sum,n,m,maxx,rres;
int vis[20][20];
char mp[20][20];
int d[10];
queue<node> q;
int BFS(int gg)
{
	int s=0;
	vis[q.front().x][q.front().y]=gg;
	while (!q.empty())
	{
		node k=q.front();
		q.pop();
		for (int i=0;i<4;i++)
		{
			int va=k.x+xx[i];
			int vb=k.y+yy[i];
			if (vis[va][vb]) continue;
			if (mp[va][vb]=='.' || mp[va][vb]=='C')
			{
				//if (vis[va][vb]==0)
				vis[va][vb]=gg;
				q.push(node(va,vb));
				if(mp[va][vb]=='C')
					s++;
			}
		}
	}
	return s;
}
void back(int x,int y,int gg)
{
	vis[x][y]=0;
    for (int i=0;i<4;i++)
	{
        int sx=x+xx[i];
        int sy=y+yy[i];
        if (sx<1 || sy<1 || sx>n || sy>m) continue;
        if (vis[sx][sy]!=gg) continue;
        back(sx,sy,gg);
    }
}
void go(int num) //第几个石头 
{
	if (num>=sum) return;
	for (int i=0;i<sum;i++)
	{
		if (d[i]) continue;
		d[i]=1;
		int x=st[i].x;
		int y=st[i].y;
		for (int j=0;j<4;j++)
		{
			int sx=x-xx[j];
			int sy=y-yy[j];
			if (sx<1 || sx>n || sy<1 || sy>m) continue;
			if (!vis[sx][sy]) continue;
			
			int tx=x+xx[j];
			int ty=y+yy[j];
			if (tx<1 || tx>n || ty<1 || ty>m) continue;
			if (mp[tx][ty]!='.' && !vis[tx][ty]) continue;
			
			char c=mp[tx][ty];
			mp[tx][ty]='O';
			mp[x][y]='.';
			q.push(node(x,y));
			rres=0;
			int res=BFS(i+2);
			maxx+=res;
			ans=max(maxx,ans);
			go(num+1);
			back(x,y,i+2);
			maxx-=res;
			mp[x][y]='O';
			mp[tx][ty]=c;
		}
		go(num+1);
		d[i]=0;
	}
}
int main()
{
	int i,j;
	scanf("%d",&T);
	while (T--)
	{
		scanf("%d%d",&n,&m);
		sum=0;
		memset(vis,0,sizeof(vis));
		memset(mp,0,sizeof(mp));
		while (!q.empty()) q.pop();
		//getchar();
		for (i=1;i<=n;i++)
		{
			//getchar();
			//scanf("%s",&mp[i]+1);
			cin>>mp[i]+1;
			for (j=1;j<=m;j++)
			{
				if (mp[i][j]=='S')
				{
					q.push(node(i,j));
					vis[i][j]=1;
					mp[i][j]='.';
				}
				if (mp[i][j]=='O')
				{
					st[sum].x=i;
					st[sum].y=j;
					sum++;			
				}
			}
		}
		/*for (i=1;i<=n;i++)
		{
			for (j=1;j<=m;j++)
			cout<<mp[i][j];
			cout<<endl;
		}*/
		memset(d,0,sizeof(d));
		rres=0;
		int f1=BFS(1);
		maxx=0;ans=0;
		go(0);
		ans+=f1;
		cout<<ans<<endl;
	}
	return 0;
}
/*
6
3 4
S.OC
..O.
.XCX
4 6
S.X.CC
..XOCC
...O.C
....XC
4 4
.SXC
OO.C
..XX
.CCC
4 7
S.XX..X
..XC.CX
...XOOC
.......
4 4
SCXC
.CXC
.OCC
CCX.
*/



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值