bfs+dfs

很复杂的搜索题。

因为数据很小,rock最多只有5个,coin最多只有10个,移动rock最多4^5=1024种状态;

思路:

  每次先把当前状态能拿到的coin拿走,并将地图当前位置设为'.' (拿走coin的位置为空)

  拿走coin后,在搜索一次,碰到rock判断是否能push动,能的话建立一个新地图,rock所在点设为'.' (空),rock移动到的点设为'X' (只能移动一次)。就这样递归下去,因为只有5个rock,最对递归5层。

  每次扫描完当前状态地图和以前拿到的最大值比较一下,取较大值 (有时候不移动rock拿到的coin更多!)

ps:开始为了少设置一个变量偷懒,结果得不偿失,卡了好久。。。看来以后要小心点,不要随便改动前面的值,宁可多敲点也别犯这种低级错误。。。

 #include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std; char map[][][];  //第一维其实只要5就够了,当时一看状态1024就没多想,反正1024也没关系,数据小~~
int visit[][][];
int n,m,ma; int dir[][]={,,,-,,,-,}; struct node {
int x,y;
}st; void cpy (int d,int s){
for (int i=;i<n;i++)
for (int j=;j<m;j++)
map[d][i][j]=map[s][i][j];
} void rvisit (int o){
for (int i=;i<n;i++)
for (int j=;j<m;j++)
visit[o][i][j]=;
} int bfs (int o,int ans,node st){//cout<<st.x<<" "<<st.y<<endl;
node a,b,c;
queue<node> q;
while (!q.empty())
q.pop ();
q.push (st);
rvisit (o);
visit[o][st.x][st.y]=;
while (!q.empty ()){
a=q.front ();
q.pop ();
int xx,yy;
for (int i=;i<;i++){
xx=a.x+dir[i][];
yy=a.y+dir[i][];
if (visit[o][xx][yy])
continue ;
if (xx<||xx>=n||yy<||yy>=m)
continue ;
if (map[o][xx][yy]=='X'||map[o][xx][yy]=='O')
continue ;
if (map[o][xx][yy]=='C'){
ans++;
map[o][xx][yy]='.';
}
b.x=xx;b.y=yy;
q.push (b);
visit[o][xx][yy]=;
}
}
ma=max (ma,ans);
q.push (st);
rvisit (o);
visit[o][st.x][st.y]=;
while (!q.empty ()){
a=q.front ();//if (o==0)cout<<o<<":"<<a.x<<" "<<a.y<<endl;
q.pop ();
int xx,yy;
for (int i=;i<;i++){
xx=a.x+dir[i][];
yy=a.y+dir[i][];
if (visit[o][xx][yy])
continue ;
if (xx<||xx>=n||yy<||yy>=m)
continue ;
if (map[o][xx][yy]=='X')
continue ;
if (map[o][xx][yy]=='O'){
int xxx,yyy;
xxx=xx+dir[i][];
yyy=yy+dir[i][];
if (xxx<||xxx>=n||yyy<||yyy>=m)
continue ;
if (map[o][xxx][yyy]=='.'){
cpy (o+,o);
map[o+][xx][yy]='.';
map[o+][xxx][yyy]='X';//cout<<o<<":"<<a.x<<" "<<a.y<<"|"<<xx<<" "<<yy<<"|"<<xxx<<" "<<yyy<<endl;
c.x=xx;c.y=yy;
bfs (o+,ans,c);
} continue ;
}
b.x=xx;b.y=yy;//if (o==0) cout<<a.x<<" "<<a.y<<"|"<<xx<<" "<<yy<<endl;
q.push (b);
visit[o][xx][yy]=;
}
}
} int main (){
int t;
scanf ("%d",&t);
while (t--){
scanf ("%d%d",&n,&m);
for (int i=;i<n;i++){
scanf ("%s",map[][i]);
for (int j=;j<m;j++){
if (map[][i][j]=='S'){
st.x=i;st.y=j;
//map[0][i][j]='.';
}
}
}//cout<<st.x<<" "<<st.y<<endl;cout<<map[0][st.x][st.y]<<endl;
ma=;
bfs (,,st);
printf ("%d\n",ma);
}
return ;
}

CSU 1119 Collecting Coins的更多相关文章

  1. UVA 12510/CSU 1119 Collecting Coins DFS

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

  2. csuoj 1119: Collecting Coins

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec  Memo ...

  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. C 产生随机码

    #include<stdio.h>#include<malloc.h>#include<conio.h>#include<stdlib.h>#inclu ...

  2. TCP释放连接时为什么time_wait状态必须等待2MSL时间

    为什么上图中的A在TIME-WAIT状态必须等待2MSL时间呢? 第一,为了保证A发送的最后一个ACK报文能够到达B.这个ACK报文段有可能丢失,因而使处在LAST-ACK状态的B收不到对已发送的FI ...

  3. Lintcode--005(最长公共子序列)

    Given two strings, find the longest common subsequence (LCS).     最长公共子序列 Your code should return th ...

  4. LeetCode_Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given ...

  5. VS2010编译Qt程序失败------error LNK1123: 转换到 COFF 期间失败:

    error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏使用VS2010编译VC++项目的时候可能会出这个问题. 据说升级到SP1后可能问题解决,但是下载量太大,目前没有得到证实. ...

  6. How to run Tomcat without root privileges? 常规用户使用tomcat的80端口

    How to run Tomcat without root privileges? 1. The best way is to use jsvc, available as part of the  ...

  7. scriptol图像处理算法

    神奇的图像处理算法   相似图片搜索是利用数学算法,进行高难度图像处理的一个例子.事实上,图像处理的数学算法,已经发展到令人叹为观止的地步. Scriptol列出了几种神奇的图像处理算法,让我们一起来 ...

  8. 【转】Service Intent must be explicit的解决方法

    原文网址:http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 今天在学习android的Service组件的时候,在Android ...

  9. bzoj2929 [Poi1999]洞穴攀行

    Description 一队洞穴学者在Byte Mountain的Grate Cave里组织了一次训练.训练中,每一位洞穴学者要从最高的一个室到达最底下的一个室.他们只能向下走.一条路上每一个连续的室 ...

  10. SRM 598 DIV1

    A 只有3种情况:200以上的单独装,3个100的装一起,某两个u,v装一起且u+v<=300, 所以做法是从大到小判断每个大小的最大能与它装一起的是谁,最后剩下100的特判. B 第一轮如果未 ...