! HDU 4101 Ali and baba-game-(Bfs&dfs scan two-dimensional points)

Source: Internet
Author: User

Test instructions: The goal is to get a value of 1 of the gem, but the premise is to have a two-dimensional lattice from the outside to the gem of the path, if there is no need to choose a stone at a time to reduce its value by 1, until the stone value of 0 becomes the pathway, the premise of the choice of this stone is also a lattice outside to the Two people take turns, each time choose a stone, a starts first, asks by the input condition, who can win.

Analysis:

Game problem, the two people will surround the stone around a circle of stones to smash all the stones and then choose this lap stone, so it is to calculate this circle of stones outside the value of all the sum of the stone, plus the value of this circle of stone minus one sum, through the odd couple can judge who wins.

Implementation method: From Gem Dfs find a circle of stone enclosing it, with vis[][] array tag This includes the range of this circle of stone, and then from the outside of this ring BFS scan again.

The use of BFS and DFS is still extensive and continues to be practiced by practicing an in-depth understanding of its application .

In addition cin cout timed out = =

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; int N,m,a[1000][1000];int D[4][2]={{-1,0},{1,0},{0,-1},{0,1}};int Vis1[1000][1000],vis2[1000][1000];int ans;struct Node{int x, y;}; queue<node> q;bool dfs (int x,int y) {if (x==0| | x==n-1| | y==0| | Y==M-1) return true;for (int i=0;i<4;i++) {int Dx=x+d[i][0];int dy=y+d[i][1];if (dx>=0&&dx<n&    &dy>=0&&dy<m&&!vis1[dx][dy]) {vis1[dx][dy]=1; if (a[dx][dy]<=0) if (Dfs (Dx,dy)) return true;}} return false;} void BFs () {while (!q.empty ()) {node Tmp=q.front (), Q.pop (); for (int i=0;i<4;i++) {int Dx=tmp.x+d[i][0];int dy=tmp.y+d I [1];if (Dx>=0&&dx<n&&dy>=0&&dy<m&&!vis2[dx][dy]) {vis2[dx][dy]=1;if ( Vis1[dx][dy]) Ans+=a[dx][dy]-1;else{ans+=a[dx][dy];node Tmp2;tmp2.x=dx,tmp2.y=dy;q.push (TMP2);}}}} int main () {while (scanf ("%d%d", &n,&m)!=eof) {int sx,sy;for (int. i=0;i<n;i++) for (int j=0;j<m;j++) {scanf ( "%d ", &a[i][j]); if (a[i][j]==-1) sx=i,sy=j;} memset (vis1,0,sizeof (VIS1)), memset (vis2,0,sizeof (VIS2)), if (Dfs (Sx,sy)) printf ("Ali win\n"); else{ans=0;for (int i=0 ; i<n;i++) for (int j=0;j<m;j++) {if (!vis2[i][j]) {if (i==0| | i==n-1| | j==0| |      j==m-1) {vis2[i][j]=1;      if (!vis1[i][j]) {while (!q.empty ()) Q.pop ();      ANS+=A[I][J];      node tmp;      Tmp.x=i,tmp.y=j;      Q.push (TMP);      BFS ();      } else ans+=a[i][j]-1; }}}if (ans%2==1) printf ("Ali win\n"), Else printf ("Baba win\n");}}}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

! HDU 4101 Ali and baba-game-(Bfs&dfs scan two-dimensional points)

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.