P1902 Assassination Ambassador Wide Search + Two Points

Topic: Give a picture, each location has a damage value.

   Now we need to walk from the first row to the last row, and every square will damage (you can start from any position on the first row)

   The final value of the injury is the maximum value of all injuries received by the person

   Question: What is the minimum value of all the columns in the last row;

Idea: According to the data range given by the title, the complexity required to traverse the entire graph is 1e6

   But one traversal is unlikely to come to an answer;

   Seeing this problem of seeking the minimum value or the maximum value, we can think about this in the past.

   This question is indeed a dichotomy. After we determine the range, we take the damage value as the object, and then make a dichotomy according to the judgment condition of Guangsou.

   So, let's talk about this judgment condition in detail

      When we determine a damage value to traverse the graph, if we encounter a damage value greater than this, we will skip,

      Go through all the points that can go, until you can go to one of the last columns and then you can exit (because all the numbers in the last row are 0)

      If you can't go to the last line, you are not satisfied

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e3+10;
 4 int a[maxn][maxn];
 5 int vis[maxn][maxn];
 6 int nxt[4][2]={ {1,0},{-1,0},{0,1},{0,-1}  };
 7 struct node
 8 {
 9     int x,y;
10 };
11 int n,m;
12 int check(int mid)
13 {
14     queue<node>q;
15     node tmp;
16     memset(vis,0,sizeof(vis));
17     for(int i=1;i<=m;i++){
18         tmp.x=1;
19         tmp.y=i;
20         q.push(tmp);
21         vis[1][i]=1;
22     }
23     while(!q.empty()){
24         tmp=q.front();
25       //  printf("hahahahhaha:%d %d\n",tmp.x,tmp.y);
26         q.pop();
27         node step;
28         for(int i=0;i<=3;i++){
29             step.x=tmp.x+nxt[i][0];
30             step.y=tmp.y+nxt[i][1];
31             if(step.x>n||step.x<=0||step.y<=0||step.y>m) continue;
32             if(a[step.x][step.y]>mid) continue;
33             if(vis[step.x][step.y]) continue;
34             q.push(step);
35             vis[step.x][step.y]=1;
36             if(step.x==n) return 1;
37         }
38     }
39     return 0;
40 }
41 int main()
42 {
43     scanf("%d%d",&n,&m);
44     for(int i=1;i<=n;i++)
45     for(int j=1;j<=m;j++){
46         scanf("%d",&a[i][j]);
47     }
48     int L=0,R=1000;
49     int ans;
50     while(L<=R){
51         int mid=L+R>>1;
52       //  printf("%d\n",mid);
53         if(check(mid)){
54             R=mid-1;
55             ans=mid;
56         }
57         else{
58             L=mid+1;
59         }
60     }
61     printf("%d\n",ans);
62     return 0;
63 }
View Code

 

Guess you like

Origin www.cnblogs.com/pangbi/p/12689898.html