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 ;
}

csuoj 1119: Collecting Coins的更多相关文章

  1. CSU 1119 Collecting Coins

    bfs+dfs 很复杂的搜索题. 因为数据很小,rock最多只有5个,coin最多只有10个,移动rock最多4^5=1024种状态: 思路: 每次先把当前状态能拿到的coin拿走,并将地图当前位置设 ...

  2. UVA 12510/CSU 1119 Collecting Coins DFS

    前年的省赛题,难点在于这个石头的推移不太好处理 后来还是看了阳神当年的省赛总结,发现这个石头这里,因为就四五个子,就暴力dfs处理即可.先把石头当做普通障碍,进行一遍全图的dfs或者bfs,找到可以找 ...

  3. Codeforces D. Sorting the Coins

    D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...

  4. codeforces 876 D. Sorting the Coins

    http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...

  5. D. Sorting the Coins

    Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins togeth ...

  6. ACM-ICPC (10/16) Codeforces Round #441 (Div. 2, by Moscow Team Olympiad)

    A. Trip For Meal Winnie-the-Pooh likes honey very much! That is why he decided to visit his friends. ...

  7. 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)

    A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...

  8. Codeforces Round #615 (Div. 3)

    A. Collecting Coins 题目链接:https://codeforces.com/contest/1294/problem/A 题意: 你有三个姐妹她们分别有 a , b , c枚硬币, ...

  9. Codeforces Round#615 Div.3 解题报告

    前置扯淡 真是神了,我半个小时切前三题(虽然还是很菜) 然后就开始看\(D\),不会: 接着看\(E\),\(dp\)看了半天,交了三次还不行 然后看\(F\):一眼\(LCA\)瞎搞,然后\(15m ...

随机推荐

  1. CSS3 justify 文本两端对齐

    浏览器参照基准:Firefox4 and Later, Chrome5 and Later, Safari5 and Later, Opera10.53 and Later, IE5.5 and La ...

  2. HDU5288 OO’s Sequence

    Problem Description OO has got a array A of size n ,defined a function f(l,r) represent the number o ...

  3. git stash和git stash pop

    git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug,  先stash, 使返回到自己上一个comm ...

  4. SOAPUI使用教程-MockOperations和响应

    如前所述,一个MockService有多个MockOperations其中每个可以包含任意数量的MockResponse消息; 也就是说,一个MockService响应实际上包括若干预设响应之间发生变 ...

  5. Unity Lightmap动态加载研究

    什么情况下需要Lightmap? 移动平台上目前暂时还不能开实时光影效果,会卡成幻灯片.所以就需要将光影烘焙到贴图上. 什么情况下需要动态加载Lightmap? 1.当项目抛弃了Unity的多场景模式 ...

  6. 【编程篇】C++11系列之——临时对象分析

    /*C++中返回一个对象时的实现及传说中的右值——临时对象*/ 如下代码: /**********************************************/ class CStuden ...

  7. mysql cpu和内存监控

    mysqlMem 监控:#!/bin/bashPid=`/bin/ps -ef|grep mysqld|grep -Ev "grep|safe"|awk '{print $2}'` ...

  8. 从零开始编写自己的C#框架(3)——开发规范(转)

    由于是业余时间编写,而且为了保证质量,对写出来的东西也会反复斟酌,所以每周只能更新两章左右,请大家谅解,也请大家耐心等待,谢谢大家的支持. 初学者应该怎样学习本系列内容呢?根据我自己的学习经验,一般直 ...

  9. 基于apache的tomcat负载均衡和集群配置

    最近不是很忙,用零碎时间做点小小的实验. 以前公司采用F5负载均衡交换机,F5将请求转发给多台服务器,每台服务器有多个webserver实例,每个webserver分布在多台服务器,交叉式的分布集群. ...

  10. vsftp 搭建及虚拟账号配置

    安装vsftpd yum -y install vsftpd chkconfig vsftpd on 修改主配置文件 vi /etc/vsftpd/vsftpd.conf # 允许匿名用户登陆,登陆时 ...