HDU 4090 GemAnd Prince (DFS + BFS)/(DFS + DFS)

Source: Internet
Author: User
Tags acos

I believe that everyone has been playing anti-spam, and even three identical colors can be eliminated. This question also adds another condition, after each elimination, the system will fall down and move left to ask how many points you can score. Question: At the beginning, my first thought was BFS + DFS. Then, I decided to use MLE and finally read other people's code. Basically, it was DFS + DFS or DFS + BFS, why is my thinking different? First, let's get an error code BFS + DFS: (Starting MLE and WA)

 #include <iostream>  #include <cstdio>  #include <cstdlib>  #include <cmath>  #include <cstring>  #include <string>  #include <vector>  #include <list>  #include <deque>  #include <queue>  #include <iterator>  #include <stack>  #include <map>  #include <set>  #include <algorithm>  #include <cctype>  using namespace std;    typedef long long LL;  const int N=2005;  const int M=555555;  const LL II=100000000;  const int INF=0x3f3f3f3f;  const double PI=acos(-1.0);    int n,m,k;  int Max;  int cnt;  bool vis[10][10];  int t[8][2]={-1,-1, -1,0, -1,1, 0,-1, 0,1, 1,-1, 1,0, 1,1};  int xiao[10][10];  int leixing;  struct xh  {      int value;      short int s[8][8];  }w,e,q[M];    void prin()  {      cout<<"************"<<endl;      for(int i=0;i<n;i++)      {          for(int j=0;j<m;j++)              cout<<w.s[i][j]<<" ";          cout<<endl;      }      cout<<"************"<<endl;  }    void dfs(int i,int j)  {      for(int k=0;k<8;k++)      {          int x=i+t[k][0];          int y=j+t[k][1];          if(x<0||x>=n||y<0||y>=m)    continue;          if(vis[x][y]||w.s[x][y]!=leixing)   continue;          vis[x][y]=1;          w.s[x][y]=0;          cnt++;          dfs(x,y);      }  }    void yidong()  {      int i,j;      for(i=0;i<m;i++)      {          int k=n-1;          for(j=n-1;j>=0;j--)          {              if(w.s[j][i]!=0)              {                  w.s[k][i]=w.s[j][i];                  if(k!=j)  w.s[j][i]=0;                  k--;              }          }      }      int k=0;      for(j=0;j<m;j++)      {          if(w.s[n-1][j]!=0)          {              if(j!=k)              {                  for(i=0;i<n;i++)                      w.s[i][k]=w.s[i][j],w.s[i][j]=0;              }                k++;          }      }  }    void prine()  {      cout<<"************"<<endl;      for(int i=0;i<n;i++)      {          for(int j=0;j<m;j++)              cout<<e.s[i][j]<<" ";          cout<<endl;      }      cout<<"************"<<endl;  }    int maxscore()  {      int sum=0,i,j;      int ll[8]={0};      for(i=0;i<n;i++)          for(j=0;j<m;j++)              ll[e.s[i][j]]++;      for(i=1;i<=k;i++)          if(ll[i]>=3)              sum+=ll[i]*ll[i];      return sum;  }    void BFS()  {      int he=0,ta=0;      int i,j;      w.value=0;      q[ta++]=w;      while(he!=ta)      {          e=q[he++];          if(he==M)              he=0;          if((maxscore()+e.value)<=Max)              continue;          memset(vis,0,sizeof(vis));          for(i=0;i<n;i++)          {              for(j=0;j<m;j++)              {                  w=e;                  if(vis[i][j]||w.s[i][j]==0)   continue;                  leixing=w.s[i][j];                  w.s[i][j]=0;                  vis[i][j]=1;                  cnt=1;                  dfs(i,j);                  if(cnt<3)   continue;                  //prin();                  yidong();//prin(); system("pause");                  w.value+=cnt*cnt;                  if(Max<w.value)                      Max=w.value;                  q[ta++]=w;                  if(ta==M)                      ta=0;              }          }      }    }    int main()  {      while(~scanf("%d%d%d",&n,&m,&k))      {          int i,j;          for(i=0;i<n;i++)              for(j=0;j<m;j++)                  scanf("%d",&w.s[i][j]);          Max=0;          BFS();          cout<<Max<<endl;      }      return 0;  }  

 

AC code: (DFS + DFS)
# Include <iostream> # include <cstdio> # include <cstdlib> # include <cmath> # include <cstring> # include <string> # include <vector> # include <list> # include <deque> # include <queue> # include <iterator> # include <stack> # include <map> # include <set> # include <algorithm> # include <cctype> using namespace std; typedef long LL; const int N = 2005; const int M = 555555; const ll ii = 100000000; const int IN F = 0x3f3f3f; const double PI = acos (-1.0); int n, m, k; int Max, cnt; int w [8] [8]; int t [8] [2] = {-1,-1,-,-, 0,-1, 1,-1 }; int leixing; inline int maxscore () {int sum = 0, I, j; int ll [8]; memset (ll, 0, sizeof (ll )); for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) ll [w [I] [j] ++; for (I = 1; I <= k; I ++) if (ll [I]> = 3) sum + = ll [I] * ll [I]; return sum;} inline void yidong () {int I, j; for (I = 0; I <m; I ++) {int k = n-1; For (j = n-1; j> = 0; j --) {if (w [j] [I]! = 0) {w [k] [I] = w [j] [I]; if (k! = J) w [j] [I] = 0; k -- ;}} int k = 0; for (j = 0; j <m; j ++) {if (w [n-1] [j]! = 0) {if (j! = K) {for (I = 0; I <n; I ++) w [I] [k] = w [I] [j], w [I] [j] = 0;} k ++ ;}} void dfs (int I, int j, bool vis [] [8]) {for (int k = 0; k <8; k ++) {int x = I + t [k] [0]; int y = j + t [k] [1]; if (x <0 | x> = n | y <0 | y> = m) continue; if (vis [x] [y] | w [x] [y]! = Leixing) continue; vis [x] [y] = 1; w [x] [y] = 0; cnt ++; dfs (x, y, vis );}} inline void debug (int p [] [8]) {cout <"***************" <endl; for (int I = 0; I <n; I ++) {for (int j = 0; j <m; j ++) cout <p [I] [j] <"; cout <endl ;} cout <"**************" <endl;} inline void save (int sa [] [8]) {for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) sa [I] [j] = w [I] [j];} inline void read (int sa [] [8]) {for (int I = 0; I <n; I ++) for (int j = 0; j <m; j ++) W [I] [j] = sa [I] [j];} void DFS (int value) {if (maxscore () + value) <= Max) // pruning return; bool vis [8] [8]; memset (vis, 0, sizeof (vis); for (int I = 0; I <n; I ++) {for (int j = 0; j <m; j ++) {if (w [I] [j] = 0) vis [I] [j] = 1; if (vis [I] [j]) continue; int sa [8] [8]; save (sa ); // save leixing = w [I] [j]; w [I] [j] = 0; cnt = 1; vis [I] [j] = 1; dfs (I, j, vis); if (cnt <3) {read (sa); continue;} yidong (); int tempv = value + cnt * cnt; if (tempv> Max) M Ax = tempv; DFS (tempv); read (sa); // read }}int main () {while (~ Scanf ("% d", & n, & m, & k) {int I, j; for (I = 0; I <n; I ++) for (j = 0; j <m; j ++) scanf ("% d", & w [I] [j]); Max = 0; DFS (0); cout <Max <endl;} return 0 ;} /* 2 2 2 1 1 1 2 3 3 3 1 3 1 1 2 1 1 1 2 2 5 4 3 3 3 1 3 3 3 3 2 2 2 2 3 3 3 3 3 3 3 3 3 3 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 3 1 2 2 */

 

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.