csuoj 1119: Collecting Coins
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
Input
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的更多相关文章
- CSU 1119 Collecting Coins
bfs+dfs 很复杂的搜索题. 因为数据很小,rock最多只有5个,coin最多只有10个,移动rock最多4^5=1024种状态: 思路: 每次先把当前状态能拿到的coin拿走,并将地图当前位置设 ...
- UVA 12510/CSU 1119 Collecting Coins DFS
前年的省赛题,难点在于这个石头的推移不太好处理 后来还是看了阳神当年的省赛总结,发现这个石头这里,因为就四五个子,就暴力dfs处理即可.先把石头当做普通障碍,进行一遍全图的dfs或者bfs,找到可以找 ...
- Codeforces D. Sorting the Coins
D. Sorting the Coins time limit per test 1 second memory limit per test 512 megabytes input standard ...
- codeforces 876 D. Sorting the Coins
http://codeforces.com/contest/876/problem/D D. Sorting the Coins time limit per test 1 second memory ...
- D. Sorting the Coins
Recently, Dima met with Sasha in a philatelic store, and since then they are collecting coins togeth ...
- 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. ...
- 湖南省第八届大学生计算机程序设计竞赛(A,B,C,E,F,I,J)
A 三家人 Description 有三户人家共拥有一座花园,每户人家的太太均需帮忙整理花园.A 太太工作了5 天,B 太太则工作了4 天,才将花园整理完毕.C 太太因为正身怀六甲无法加入她们的行列, ...
- Codeforces Round #615 (Div. 3)
A. Collecting Coins 题目链接:https://codeforces.com/contest/1294/problem/A 题意: 你有三个姐妹她们分别有 a , b , c枚硬币, ...
- Codeforces Round#615 Div.3 解题报告
前置扯淡 真是神了,我半个小时切前三题(虽然还是很菜) 然后就开始看\(D\),不会: 接着看\(E\),\(dp\)看了半天,交了三次还不行 然后看\(F\):一眼\(LCA\)瞎搞,然后\(15m ...
随机推荐
- CSS3 justify 文本两端对齐
浏览器参照基准:Firefox4 and Later, Chrome5 and Later, Safari5 and Later, Opera10.53 and Later, IE5.5 and La ...
- 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 ...
- git stash和git stash pop
git stash 可用来暂存当前正在进行的工作, 比如想pull 最新代码, 又不想加新commit, 或者另外一种情况,为了fix 一个紧急的bug, 先stash, 使返回到自己上一个comm ...
- SOAPUI使用教程-MockOperations和响应
如前所述,一个MockService有多个MockOperations其中每个可以包含任意数量的MockResponse消息; 也就是说,一个MockService响应实际上包括若干预设响应之间发生变 ...
- Unity Lightmap动态加载研究
什么情况下需要Lightmap? 移动平台上目前暂时还不能开实时光影效果,会卡成幻灯片.所以就需要将光影烘焙到贴图上. 什么情况下需要动态加载Lightmap? 1.当项目抛弃了Unity的多场景模式 ...
- 【编程篇】C++11系列之——临时对象分析
/*C++中返回一个对象时的实现及传说中的右值——临时对象*/ 如下代码: /**********************************************/ class CStuden ...
- mysql cpu和内存监控
mysqlMem 监控:#!/bin/bashPid=`/bin/ps -ef|grep mysqld|grep -Ev "grep|safe"|awk '{print $2}'` ...
- 从零开始编写自己的C#框架(3)——开发规范(转)
由于是业余时间编写,而且为了保证质量,对写出来的东西也会反复斟酌,所以每周只能更新两章左右,请大家谅解,也请大家耐心等待,谢谢大家的支持. 初学者应该怎样学习本系列内容呢?根据我自己的学习经验,一般直 ...
- 基于apache的tomcat负载均衡和集群配置
最近不是很忙,用零碎时间做点小小的实验. 以前公司采用F5负载均衡交换机,F5将请求转发给多台服务器,每台服务器有多个webserver实例,每个webserver分布在多台服务器,交叉式的分布集群. ...
- vsftp 搭建及虚拟账号配置
安装vsftpd yum -y install vsftpd chkconfig vsftpd on 修改主配置文件 vi /etc/vsftpd/vsftpd.conf # 允许匿名用户登陆,登陆时 ...