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. myBaties 和 mysql开发中遇到的问题

    最近开发内部平台遇到mysql 中的一个问题,order by语句需要在limit 之后. myBaties在parameterType="java.lang.String" 不能 ...

  2. 用Less CSS定义常用的CSS3效果函数

    定义圆角及调用 /* 定义圆角 @radius 圆角大小 */ .round(@radius:5px){ border-radius:@radius; -webkit-border-radius: @ ...

  3. 2016 CCPC 东北地区重现赛

    1. 2016 CCPC 东北地区重现赛 2.总结:弱渣,只做出01.03.05水题 08   HDU5929 Basic Data Structure    模拟,双端队列 1.题意:模拟一个栈的操 ...

  4. ImageLoader

    配置ImageLoader 一般我们在使用ImageLoader的时候,需要在应用程序的入口进行它的一个配置,这个配置一般写到Application里边 * public void initImage ...

  5. 配置apache虚拟域名

    Apache配置文件的修改.   ----> Apache-----> httpd.conf,打开httpd.conf文件.   1)找到:#LoadModule rewrite_modu ...

  6. HTML基础篇之图像热区补充一下图片相对地址的定义

    HTML5课堂笔记理解2 上次说到图片相对地址的定义,举例了4中情形的下的不同目录的图片书写方法补充一个如果你要的图片目录跟上面四种都不一样话可以用以下属性值尝试 ./            当前目录 ...

  7. 【emWin】例程二:显示“hello,world”

    实验指导书及代码包下载: http://pan.baidu.com/s/1c1Csx48

  8. myBatis的一对多查询,主要利用resultMap实现一次查询多个结果集

    日常开发中有这中场景,一个用户有多个角色,一个角色又有多个菜单,想查出一个用户的所有菜单.除了常见的关联查询之外,更使用的应该是利用myBatis的resultMap来实现一次查询出多个结果集,缺点: ...

  9. web系统登陆页面增加验证码

    传统登陆页面中包含两个输入项: • 用户名 • 密码有时为了防止机器人进行自动登陆操作,或者防止恶意用户进行用户信息扫描,需增加动态验证码功能.此时,登陆页面中包含了三个输入项: • 用户名 • 密码 ...

  10. Codevs 3728 联合权值

    问题描述 无向连通图G有n个点,n-1条边.点从1到n依次编号,编号为i的点的权值为Wi ,每 条边的长度均为1.图上两点(u,v)的距离定义为u点到v点的最短距离.对于图G上的点 对(u,v),若它 ...