码迷,mamicode.com
首页 > 其他好文 > 详细

【luogu P1514 引水入城】 题解

时间:2018-04-11 21:44:29      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:main   def   col   www   tle   get   style   scan   new   

题目链接:https://www.luogu.org/problemnew/show/P1514

 1 // luogu-judger-enable-o2
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #define maxn 520
 7 #define inf 2147483647
 8 using namespace std;
 9 int n, m, cnt = 0, cur1 = 0, flag, ma[maxn][maxn], vis[maxn][maxn] = {0}, q[maxn];
10 struct cover
11 {
12     int l,r;
13 }c[maxn];
14 int cmp(cover a, cover b)
15 {
16     if(a.l != b.l)
17     return a.l < b.l;
18     else return a.r > b.r;
19 }
20 void dfs(int x, int y, int now)
21 {
22     vis[x][y] = 1;
23     if(x == n)
24     {
25         q[y] = 1;
26         c[now].l = min(c[now].l , y);
27         c[now].r = max(c[now].r , y);
28     }
29 
30     if(ma[x+1][y] < ma[x][y] && x+1 <= n && vis[x+1][y] == 0) dfs(x+1,y,now);
31 
32     if(ma[x-1][y] < ma[x][y] && x-1 >= 1 && vis[x-1][y] == 0) dfs(x-1,y,now); 
33 
34     if(ma[x][y+1] < ma[x][y] && y+1 <= m && vis[x][y+1] == 0) dfs(x,y+1,now);
35 
36     if(ma[x][y-1] < ma[x][y] && y-1 >= 1 && vis[x][y-1] == 0) dfs(x,y-1,now);  
37 }
38 int main()
39 {
40     scanf("%d%d",&n,&m);
41     for(int i = 1; i <= m; i++) 
42     c[i].l = inf;
43 
44     for(int i = 1; i <= n; i++)
45     for(int j = 1; j <= m; j++)
46     {
47         scanf("%d",&ma[i][j]);
48     }
49 
50     for(int i = 1; i <= m; i++)
51     {
52         if(ma[1][i]>=ma[1][i-1])
53         dfs(1,i,i);
54         memset(vis,0,sizeof(vis));
55     }
56 
57     for(int i=1;i<=m;i++)
58     {
59         if(q[i] == 0)
60         flag++;
61         //cout<<q[i]<<" ";
62     }
63    
64     sort(c+1,c+1+m,cmp);
65     
66     if(flag != 0)
67     printf("0\n%d",flag);
68     else
69     {
70         int cur = c[1].r;
71         cnt++;
72         for(int i = cur; i <= m; i = cur1)
73         {
74             if(i == m)
75             break;
76             
77             cnt++;
78             
79             for(int j = 1; c[j].l <= i+1; j++)
80             cur1 = max(cur1, c[j].r);
81         }
82         printf("1\n%d",cnt);
83     }
84     return 0;
85 }

 

【luogu P1514 引水入城】 题解

标签:main   def   col   www   tle   get   style   scan   new   

原文地址:https://www.cnblogs.com/MisakaAzusa/p/8798396.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!