引水入城

原题链接:https://www.luogu.org/problem/show?pid=1514

搜索+贪心(虽然好多题解都是DP)

这样写只能拿90,鬼知道是什么原因。

参考了一篇题解(原题解也是有误的,一样只能拿90)

思路和洛谷题解上的一个是一样的。。。大家去看那个就行。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <queue>
 5 #include <algorithm>
 6 #define maxn 520
 7 using namespace std;
 8 int n,m,ans,i,j;
 9 int cnt;
10 int a[maxn][maxn];
11 bool exist[maxn][maxn];
12 bool judge[maxn][maxn];
13 struct node{
14     int x;
15     int y;
16     bool operator<(const node &rhs)const{
17         return x < rhs.x;
18     }
19 };
20 node s[maxn];
21 int read(){
22     int num = 0;
23     char c;
24     bool flag = false;
25     while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
26     if (c == '-')
27         flag = true;
28     else
29         num = c - '0';
30     while (isdigit(c = getchar()))
31         num = num * 10 + c - '0';
32     return (flag ? -1 : 1) * num;
33 }
34 void dfs(int x,int y){
35     if (exist[x][y])
36         return;
37     exist[x][y] = true;
38     if (x+1 <= n && a[x][y] > a[x+1][y])
39         dfs(x+1,y);
40     if (x-1 >= 1 && a[x][y] > a[x-1][y]) 
41         dfs(x-1,y);
42     if (y + 1 <= m && a[x][y] > a[x][y+1]) 
43         dfs(x,y+1);
44     if (y - 1 >= 1 && a[x][y] > a[x][y-1]) 
45         dfs(x,y-1);
46 }
47 
48 int main(){
49     n = read();m = read();
50     for (i=1;i<=n;i++)
51         for (j=1;j<=m;j++)
52             a[i][j] = read();
53     for (i=1;i<=m;i++){
54         memset(exist,0,sizeof(exist));
55         dfs(1,i);
56         for (j=1;j<=m;j++)
57             judge[i][j] = exist[n][j];
58     }
59     cnt = m;
60     for (i=1;i<=m;i++)
61         for (j=1;j<=m;j++)
62             if (judge[j][i]){
63                 cnt--;
64                 break;
65             }
66     if (cnt > 0){
67         printf("0\n%d",cnt);
68         return 0;
69     }
70     for (i=1;i<=m;i++){
71         for (j=1;j<=m;j++)
72             if (judge[i][j])
73                 break;
74         s[i].x = j;
75         for (j = s[i].x + 1;j<=m;j++)
76             if (!judge[i][j])
77                 break;
78         s[i].y = j-1;
79     }
80     sort(s+1,s+m+1);
81     int maxv = 0;
82     int tmp = 0;
83     for (i=1;i<=m;i++){
84         if (s[i].x > m)
85             break;
86         if (s[i].x <= maxv + 1)
87             tmp = max(tmp,s[i].y);
88         else{
89             maxv = tmp;
90             ans++;
91             tmp = max(s[i].y,tmp);
92         }
93     }
94     if (maxv != m)
95         ans++;
96     printf("1\n%d",ans);
97     return 0;
98 }

 

posted @ 2017-10-17 23:45  ShawnZhou_Aether  阅读(223)  评论(0编辑  收藏  举报