第八届湖南省赛I题 Collecting Coins(BFS)
来源:互联网 发布:ado.net mysql 编辑:程序博客网 时间:2024/05/19 22:04
1119: Collecting Coins
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 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
33 4S.OC..O..XCX4 6S.X.CC..XOCC...O.C....XC4 4.SXCOO.C..XX.CCC
Sample Output
163
题意: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-8using 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;}/*63 4S.OC..O..XCX4 6S.X.CC..XOCC...O.C....XC4 4.SXCOO.C..XX.CCC4 7S.XX..X..XC.CX...XOOC.......4 4SCXC.CXC.OCCCCX.*/
0 0
- 第八届湖南省赛I题 Collecting Coins(BFS)
- 2012年湖南省第八届程序设计大赛 I Collecting Coins(dfs+bfs)
- 1119 Collecting Coins 湖南省第八届
- 湖南省第八届省赛题!!csu1119 Collecting Coins
- 湖南省第八届程序设计竞赛I
- 利用dfs和bfs的回溯,湖南省第八届省赛Collection Coins
- 第八届湖南省赛D题 平方根大搜索
- UVA12510 Collecting Coins
- 湖南省第八届程序设计竞赛 A
- 湖南省第八届程序设计竞赛 B
- 湖南省第八届程序设计竞赛C
- 湖南省第八届程序设计竞赛D
- 湖南省第八届程序设计竞赛E
- 湖南省第八届程序设计竞赛F
- 湖南省第八届程序设计竞赛G
- 湖南省第八届程序设计竞赛H
- 湖南省第八届程序设计竞赛J
- 湖南省第八届程序设计竞赛K
- PLSQL基础知识
- 传统的table布局和css布局的比较
- junit in action读书笔记
- linux守护进程
- 【uva】10494 If We Were a Child Again
- 第八届湖南省赛I题 Collecting Coins(BFS)
- oradebug 推进SCN
- codechef Chef and Left-Right
- JAVAEE基础
- C/C++内存分配
- 人脸检测和识别
- select函数用法
- 直方图均衡化
- 修改本机host下载android的sdk