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. Leetcode_Best Time to Buy and Sell Stock

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  2. 基于QT开发的第三方库

    基于Qt开发的第三方库 分类: Qt2014-02-12 11:34 1738人阅读 评论(0) 收藏 举报 QT第三方库   目录(?)[+]   文章来源:http://blog.csdn.net ...

  3. Qt编译慢吗?

    1. “用Qt写的程序编译比MFC慢”的说法是错误的绝对错误,单位代码行数编译Qt远比MFC快得多,因为Qt库的头文件设计非常好,尽量都使用了前置声明,避免了头文件嵌套,几乎所有类都使用了公有类和私有 ...

  4. emacs vim IDE

    原本想花点时间来学习下Vim或者emacs,结果在网上搜索到这篇文章 骂战挺多的,但是也长见识 http://bbs.csdn.net/topics/390306165 下面是windows下的ema ...

  5. 奇葩的SQL*Net more data from client等待,导致批处理巨慢

    <pre name="code" class="sql"><pre name="code" class="sql ...

  6. 【转】ubuntu14.04 trusty的源

    原文网址:http://blog.chinaunix.net/uid-15041-id-4821715.html 一.编辑更新源文件:/etc/apt/sources.list二.更新源索引文件:ap ...

  7. 3D objects key rendering steps

    Key steps of Rendering objects: 1 Create objects’ meshes, which we can use C++’s vector container to ...

  8. c语言const

    const关键字 const和指针结合,共有4种形式 const int *p; p是一个指针,指针指向一个int型数据.p所指向的是个常量. int const *p; p是一个指针,指针指向一个i ...

  9. java多个listener监听

    java 多个listener 监听方法 在class 名称上一行添加@Listeners 括号中用逗号隔开 @Listeners({com.example.MyListener.class,com. ...

  10. oracle 使用sql获取数据库表,表的字段

    --第一种方法: 查询dba_tab_columns select COLUMN_NAME,DATA_TYPE,DATA_LENGTH  from   dba_tab_columns where  t ...