CSU 1119 Collecting Coins

來源:互聯網
上載者:User

標籤:style   blog   color   os   io   ar   for   資料   div   

bfs+dfs

很複雜的搜尋題。

因為資料很小,rock最多隻有5個,coin最多隻有10個,移動rock最多4^5=1024種狀態;

思路:

  每次先把目前狀態能拿到的coin拿走,並將地圖當前位置設為‘.‘ (拿走coin的位置為空白)

  拿走coin後,在搜尋一次,碰到rock判斷是否能push動,能的話建立一個新地圖,rock所在點設為‘.‘ (空),rock移動到的點設為‘X‘ (只能移動一次)。就這樣遞迴下去,因為只有5個rock,最對遞迴5層。

  每次掃描完目前狀態地圖和以前拿到的最大值比較一下,取較大值 (有時候不移動rock拿到的coin更多!)

 

ps:開始為了少設定一個變數偷懶,結果得不償失,卡了好久。。。看來以後要小心點,不要隨便改動前面的值,寧可多敲點也別犯這種低級錯誤。。。

  1 #include <iostream>  2 #include <cstring>  3 #include <cstdio>  4 #include <algorithm>  5 #include <cmath>  6 #include <queue>  7 using namespace std;  8   9 char map[1050][20][20];  //第一維其實只要5就夠了,當時一看狀態1024就沒多想,反正1024也沒關係,資料小~~ 10 int visit[1050][20][20]; 11 int n,m,ma; 12  13 int dir[4][2]={0,1,0,-1,1,0,-1,0}; 14  15 struct node { 16     int x,y; 17 }st; 18  19 void cpy (int d,int s){ 20     for (int i=0;i<n;i++) 21         for (int j=0;j<m;j++) 22             map[d][i][j]=map[s][i][j]; 23 } 24  25 void rvisit (int o){ 26     for (int i=0;i<n;i++) 27         for (int j=0;j<m;j++) 28             visit[o][i][j]=0; 29 } 30  31 int bfs (int o,int ans,node st){//cout<<st.x<<" "<<st.y<<endl; 32     node a,b,c; 33     queue<node> q; 34     while (!q.empty()) 35         q.pop (); 36     q.push (st); 37     rvisit (o); 38     visit[o][st.x][st.y]=1; 39     while (!q.empty ()){ 40         a=q.front (); 41         q.pop (); 42         int xx,yy; 43         for (int i=0;i<4;i++){ 44             xx=a.x+dir[i][0]; 45             yy=a.y+dir[i][1]; 46             if (visit[o][xx][yy]) 47                 continue ; 48             if (xx<0||xx>=n||yy<0||yy>=m) 49                 continue ; 50             if (map[o][xx][yy]==‘X‘||map[o][xx][yy]==‘O‘) 51                 continue ; 52             if (map[o][xx][yy]==‘C‘){ 53                 ans++; 54                 map[o][xx][yy]=‘.‘; 55             } 56             b.x=xx;b.y=yy; 57             q.push (b); 58             visit[o][xx][yy]=1; 59         } 60     } 61     ma=max (ma,ans); 62     q.push (st); 63     rvisit (o); 64     visit[o][st.x][st.y]=1; 65     while (!q.empty ()){ 66         a=q.front ();//if (o==0)cout<<o<<":"<<a.x<<" "<<a.y<<endl; 67         q.pop (); 68         int xx,yy; 69         for (int i=0;i<4;i++){ 70             xx=a.x+dir[i][0]; 71             yy=a.y+dir[i][1]; 72             if (visit[o][xx][yy]) 73                 continue ; 74             if (xx<0||xx>=n||yy<0||yy>=m) 75                 continue ; 76             if (map[o][xx][yy]==‘X‘) 77                 continue ; 78             if (map[o][xx][yy]==‘O‘){ 79                 int xxx,yyy; 80                 xxx=xx+dir[i][0]; 81                 yyy=yy+dir[i][1]; 82                 if (xxx<0||xxx>=n||yyy<0||yyy>=m) 83                     continue ; 84                 if (map[o][xxx][yyy]==‘.‘){ 85                     cpy (o+1,o); 86                     map[o+1][xx][yy]=‘.‘; 87                     map[o+1][xxx][yyy]=‘X‘;//cout<<o<<":"<<a.x<<" "<<a.y<<"|"<<xx<<" "<<yy<<"|"<<xxx<<" "<<yyy<<endl; 88                     c.x=xx;c.y=yy; 89                     bfs (o+1,ans,c); 90                 } 91                  92                 continue ; 93             } 94             b.x=xx;b.y=yy;//if (o==0) cout<<a.x<<" "<<a.y<<"|"<<xx<<" "<<yy<<endl; 95             q.push (b); 96             visit[o][xx][yy]=1; 97         } 98     } 99 }100 101 int main (){102     int t;103     scanf ("%d",&t);104     while (t--){105         scanf ("%d%d",&n,&m);106         for (int i=0;i<n;i++){107             scanf ("%s",map[0][i]);108             for (int j=0;j<m;j++){109                 if (map[0][i][j]==‘S‘){110                     st.x=i;st.y=j;111                     //map[0][i][j]=‘.‘;112                 }113             }114         }//cout<<st.x<<" "<<st.y<<endl;cout<<map[0][st.x][st.y]<<endl;115         ma=0;116         bfs (0,0,st);117         printf ("%d\n",ma);118     }119     return 0;120 }

 

CSU 1119 Collecting Coins

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.