Luo Gu P3956 board solution to a problem

Daily questions day5 punch

Analysis

Deep search prune + + jb blind Analyzing

1. cross-border

2. The point colorless

3. The current value has been larger than answers

Three cases to be pruned

I searched in determining whether or not to cast judgment on the time when there is not no magic, white tune 0.5h, Oh ~ ~ ~

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<queue>
 6 #define maxn 100+10
 7 #define INF 2147483647/2-1
 8 using namespace std;
 9 inline int read() 
10 {
11     int x=0;
12     bool f=1;
13     char c=getchar();
14     for(; !isdigit(c); c=getchar()) if(c=='-') f=0;
15     for(; isdigit(c); c=getchar()) x=(x<<3)+(x<<1)+c-'0';
16     if(f) return x;
17     return 0-x;
18 }
19 inline void write(int x)
20 {
21     if(x<0){putchar('-');x=-x;}
22     if(x>9)write(x/10);
23     putchar(x%10+'0');
24 }
25 int m,n,ans=INF;
26 int map[maxn][maxn],val[maxn][maxn];
27 int d1[6]={0,1,-1,0,0},d2[6]={0,0,0,1,-1};
28 inline void dfs(intnx, int , int ns, int flag)
 29  {
 30      if (nx < 1 || nx> m || < 1 ||> m || map [nx] [the] == 0 || ns> = Val [nx] [a]) return ;
31      Val [nx] [] = ns;
32      if (nx == m && == m)
 33      {
 34          ans = min (ans, ns);
35          return ;
36      }
 37      for ( int i = 1 ; i <= 4 ; i ++ )
 38     {
39         int xx=nx+d1[i],yy=ny+d2[i];
40         if(map[xx][yy]>0&&map[nx][ny]==map[xx][yy]) dfs(xx,yy,ns,0);
41         else if(map[xx][yy]>0&&map[nx][ny]!=map[xx][yy]) dfs(xx,yy,ns+1,0);
42         else if(map[xx][yy]==0&&!flag)
43         {
44             map[xx][yy]=map[nx][ny];
45             dfs(xx,yy,ns+2,1);
46             map[xx][yy]=0;
47         }
48     }
49 }
50 int main()
51 {
52     memset(val,127,sizeof(val)); 
53     m=read();n=read();
54     for(int i=1;i<=n;i++)
55     {
56         int x,y,z;
57         x=read();y=read();z=read();
58         map[x][y]=z+1;
59     }
60     dfs(1,1,0,false);
61     if(ans==INF) write(-1);
62     else write(ans);
63     return 0;
64 }

Please Gangster treatise(Anyway, I do not know what that means treatise)

Guess you like

Origin www.cnblogs.com/handsome-zyc/p/11365445.html