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 ...
随机推荐
- CentOS 6.5 下 CDH 5.2.1 集群安装(一)
集群节点数量3 个 192.168.1.170 cdh-master 192.168.1.171 cdh-slave-1 192.168.1.171 cdh-slave-2 一.安装CentOS6.5 ...
- java基础-泛型2
浏览以下内容前,请点击并阅读 声明 6 类型推测 java编译器能够检查所有的方法调用和对应的声明来决定类型的实参,即类型推测,类型的推测算法推测满足所有参数的最具体类型,如下例所示: //泛型方法的 ...
- Mysql 5.7 使用SSL安全连接
MySQL默认的数据通道是不加密的,在一些安全性要求特别高的场景下,我们需要配置MySQL端口为SSL,使得数据通道加密处理,避免敏感信息泄漏和被篡改.当然,启用MySQL SSL之后,由于每个数据包 ...
- xp系统重绘边框线不显示(首次加载没有触发paint事件)
同样是,重绘边框事件,win7系统显示正常,而xp系统却不显示,这是什么原因造成的呢? 于是,小编开始百度,不停的查找原因,通过一番查找,小编也意外的收获了一些内容: 例如:窗口的拖动,放大,缩小,等 ...
- 工作中的sql语句总结
1,查找mysql 数据库 自动 添加 序号 字段列1,2,3,4 ) AS rowno,ip,startcount ) b 2,mysql的分页语句 limit后面第一个参数是index,从0开始: ...
- [Android]关于filed 遍历资源文件的排序问题
Field[] svgfields = R.drawable.class.getFields(); listid = new ArrayList<Integer>(); for (Fiel ...
- 折腾一两天,终于学会使用grunt压缩合并混淆JS脚本,小激动,特意记录一下+spm一点意外收获
很长时间没有更新博客了,实在是太忙啦...0.0 ,以下的东西纯粹是记录,不是我原创,放到收藏夹还担心不够,这个以后常用,想来想去,还是放到这里吧,,丢不了..最后一句废话,网上搜集也好原创也罢,能解 ...
- Log4J 配置文件全属性详解
第一步:加入log4j-1.2.8.jar到lib下. 第二步:在CLASSPATH下建立log4j.properties.内容如下: 1 log4j.rootCategory=INFO, stdou ...
- PyInstaller打包步骤简记
pyinstaller 下载地址:http://www.pyinstaller.org/ 下载后用cmd进入解压文件夹 python setup.py install 安装. 最近用pyinstall ...
- 记录一次Tomcat内存泄露原因的追溯
现象:WEB无法访问.SSH无法登陆.桌面登陆验证失败. 重启服务器后登陆正常. cat /var/log/message显示root用户创建了2000多个sessions后显示内存不足. 进入tom ...