Va 12510/CSU 1119 collecting coins DFS

Source: Internet
Author: User

The difficulty of the provincial competition in the year before was that the stone was not well handled.

Later I read the summary of Yang Shen's provincial competition and found that this stone is here, because there are only four or five sub-members, it is enough to deal with violent DFS. First, we regard the stone as a common obstacle, perform a full graph of DFS or BFs, find the available points, and then each time the DFS explores a new point in a new area, we can figure it out.

#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;}

 

Va 12510/CSU 1119 collecting coins DFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.