UVA 12510/CSU 1119 Collecting Coins DFS

前年的省赛题,难点在于这个石头的推移不太好处理

后来还是看了阳神当年的省赛总结,发现这个石头这里,因为就四五个子,就暴力dfs处理即可。先把石头当做普通障碍,进行一遍全图的dfs或者bfs,找到可以找的点,然后dfs每次探索新区域的新点即可,想通了这里很好做了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char mat[15][15];
int vis[15][15];
int rock[8][2],cnt;
int n,m;
int dir[][2]={{1,0},{-1,0},{0,1},{0,-1}};
int inq[8];
int res,sum;
void dfs1(int sx,int sy,int col)
{
    vis[sx][sy]=col;
    for (int i=0;i<4;i++){
        int nx=sx+dir[i][0];
        int ny=sy+dir[i][1];
        if (nx<0 || nx>=n || ny<0 || ny>=m) continue;
        if (vis[nx][ny]) continue;
        if (mat[nx][ny]==‘O‘|| mat[nx][ny]==‘X‘) continue;

        if(mat[nx][ny]==‘C‘){
           res++;
        }
        dfs1(nx,ny,col);
    }
}
void back(int x,int y,int col)
{
    vis[x][y]=0;
    for (int i=0;i<4;i++){
        int nx=x+dir[i][0];
        int ny=y+dir[i][1];
        if (nx<0 || ny<0 || nx>=n || ny>=m) continue;
        if (vis[nx][ny]!=col) continue;
        back(nx,ny,col);
    }

}
int maxn,ans;
void proc(int num)
{
    char cc;
    if (num>=cnt) return;
    for (int i=0;i<cnt;i++){
        if (inq[i]) continue;
        inq[i]=1;
        int x=rock[i][0];
        int y=rock[i][1];
        for (int j=0;j<4;j++){
            int dx=x+dir[j][0];
            int dy=y+dir[j][1];
            if (dx<0 || dx>=n || dy<0 || dy>=m) continue;
            if (mat[dx][dy]==‘X‘) continue;
            if (!vis[dx][dy]) continue;
            int tx=x-dir[j][0];
            int ty=y-dir[j][1];
            if (tx<0 || tx>=n || ty<0 || ty>=m) continue;
            if (mat[tx][ty]!=‘.‘ && !vis[tx][ty]) continue;
            cc=mat[tx][ty];
            mat[tx][ty]=‘O‘;
            mat[x][y]=‘.‘;
            res=0;
            dfs1(x,y,i+2);
            maxn+=res;
            int tmp=res;
            ans=max(ans,maxn);
            proc(num+1);
            back(x,y,i+2);
            maxn-=tmp;
            mat[tx][ty]=cc;
            mat[x][y]=‘O‘;
        }
        proc(num+1);
        inq[i]=0;
    }

}
int main()
{
    int t,sx,sy;
    scanf("%d",&t);
    while (t--)
    {
        cnt=0;
        scanf("%d%d",&n,&m);
        for (int i=0;i<n;i++){
            scanf("%s",mat[i]);
            for (int j=0;j<m;j++){
                vis[i][j]=0;
                if (mat[i][j]==‘S‘){
                    sx=i;sy=j;mat[i][j]=‘.‘;
                }
                else
                if (mat[i][j]==‘O‘){
                    rock[cnt][0]=i;
                    rock[cnt++][1]=j;
                }
            }
        }
        res=0;
        dfs1(sx,sy,1);
        sum=res;
        maxn=0,ans=0;
        memset(inq,0,sizeof inq);
        proc(0);
        ans+=sum;
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-08-02 22:18:57

UVA 12510/CSU 1119 Collecting Coins DFS的相关文章

CSU 1119 Collecting Coins

bfs+dfs 很复杂的搜索题. 因为数据很小,rock最多只有5个,coin最多只有10个,移动rock最多4^5=1024种状态: 思路: 每次先把当前状态能拿到的coin拿走,并将地图当前位置设为'.' (拿走coin的位置为空) 拿走coin后,在搜索一次,碰到rock判断是否能push动,能的话建立一个新地图,rock所在点设为'.' (空),rock移动到的点设为'X' (只能移动一次).就这样递归下去,因为只有5个rock,最对递归5层. 每次扫描完当前状态地图和以前拿到的最大值比

csuoj 1119: Collecting Coins

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1119 1119: Collecting Coins Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 144  Solved: 35[Submit][Status][Web Board] Description In a maze of r rows and c columns, your task is to collect as many coin

uva 219 - Department of Redundancy Department(dfs+剪枝)

题目链接:uva 219 - Department of Redundancy Department 题目大意:给定一些关系,问哪一些关系是可以被替代的,如果可以被替代,给出替代的方案,一种即可. 解题思路:因为总共也就26个字母,所以用二进制数表示状态.剪枝,每次将所有可选关系均考虑进去都无法满足则是false. #include <cstdio> #include <cstring> #include <algorithm> using namespace std;

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

UVA - 524 Prime Ring Problem(dfs回溯法)

UVA - 524 Prime Ring Problem Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbers  into each circle separately, and the sum of number

uva 11008 Antimatter Ray Clearcutting(DFS + 记忆化搜索)

uva 11008 Antimatter Ray Clearcutting It's year 2465, and you are the Chief Engineer for Glorified Lumberjacks Inc. on planet Trie. There is a number of trees that you need to cut down, and the only weapon you have is a high-powered antimatter ray th

uva 10344 23 out of 5 (DFS)

uva 10344 23 out of 5 Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers (1<=i<=5) that will yield the value 23. For this problem we will only consider arithmetic expression

UVa 572 - Oil Deposits【图DFS】

Oil Deposits The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It

CSU - 1356 Catch(dfs染色两种写法,和hdu4751比较)

Description A thief is running away! We can consider the city where he locates as an undirected graph in which nodes stand for crosses and edges stand for streets. The crosses are labeled from 0 to N–1. The tricky thief starts his escaping from cross