标签:
HDU 4185 :
1 6 ...... .##... .##... ....#. ....## ......
Case 1: 3
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int maxn=600+10; char s[maxn][maxn]; int mp[maxn][maxn],id[maxn][maxn]; int used[maxn],link[maxn]; int dr[][2]={-1,0,1,0,0,-1,0,1}; int t,n,cnt,cas; bool dfs(int x) { REP(i,cnt) { if(mp[x][i]&&!used[i]) { used[i]=true; if(link[i]==-1||dfs(link[i])) { link[i]=x; return true; } } } return false; } void work() { int res=0; CLEAR(link,-1); REP(i,cnt) { CLEAR(used,0); if(dfs(i)) res++; } printf("Case %d: %d\n",cas++,res/2); } bool ok(int x,int y) { if(x<0||x>=n||y<0||y>=n) return false; return true; } int main() { cas=1; scanf("%d",&t); while(t--) { scanf("%d",&n); cnt=1; CLEAR(id,0); REP(i,n) { scanf("%s",s[i]); REP(j,n) if(s[i][j]=='#') id[i][j]=cnt++;//编号 } CLEAR(mp,0); REP(i,n) { REP(j,n) { if(id[i][j]) { REP(k,4) { int xx=i+dr[k][0]; int yy=j+dr[k][1]; if(ok(xx,yy)&&id[xx][yy]) mp[id[i][j]][id[xx][yy]]=1; } } } } work(); } return 0; }
Description
The Global Aerial Research Centre has been allotted the task of building the fifth generation of mobile phone nets in Sweden. The most striking reason why they got the job, is their discovery of a new, highly noise resistant, antenna. It is called 4DAir, and
comes in four types. Each type can only transmit and receive signals in a direction aligned with a (slightly skewed) latitudinal and longitudinal grid, because of the interacting electromagnetic field of the earth. The four types correspond to antennas operating
in the directions north, west, south, and east, respectively. Below is an example picture of places of interest, depicted by twelve small rings, and nine 4DAir antennas depicted by ellipses covering them.
Obviously, it is desirable to use as few antennas as possible, but still provide coverage for each place of interest. We model the problem as follows: Let A be a rectangular matrix describing the surface of Sweden, where an entry of A either is a point of interest, which must be covered by at least one antenna, or empty space. Antennas can only be positioned at an entry in A. When an antenna is placed at row r and column c, this entry is considered covered, but also one of the neighbouring entries (c+1,r),(c,r+1),(c-1,r), or (c,r-1), is covered depending on the type chosen for this particular antenna. What is the least number of antennas for which there exists a placement in A such that all points of interest are covered? Input
On the first row of input is a single positive integer n, specifying the number of scenarios that follow. Each scenario begins with a row containing two positive integers h and w, with 1 <= h <= 40 and 0 < w <= 10. Thereafter is a matrix presented, describing
the points of interest in Sweden in the form of h lines, each containing w characters from the set [‘*‘,‘o‘]. A ‘*‘-character symbolises a point of interest, whereas a ‘o‘-character represents open space.
Output
For each scenario, output the minimum number of antennas necessary to cover all ‘*‘-entries in the scenario‘s matrix, on a row of its own.
Sample Input 2 7 9 ooo**oooo **oo*ooo* o*oo**o** ooooooooo *******oo o*o*oo*oo *******oo 10 1 * * * o * * * * * * Sample Output 17 5 Source |
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) typedef long long LL; typedef pair<int,int>pil; const int maxn=500+10; char s[maxn][maxn]; int mp[maxn][maxn],id[maxn][maxn]; int used[maxn],link[maxn]; int dr[][2]={-1,0,1,0,0,-1,0,1}; int t,n,m,cnt,cas; bool dfs(int x) { REPF(i,1,cnt) { if(mp[x][i]&&!used[i]) { used[i]=true; if(link[i]==-1||dfs(link[i])) { link[i]=x; return true; } } } return false; } void work() { int res=0; CLEAR(link,-1); REPF(i,1,cnt-1) { CLEAR(used,0); if(dfs(i)) res++; } printf("%d\n",cnt-1-res/2); } bool ok(int x,int y) { if(x<0||x>=n||y<0||y>=m) return false; return true; } int main() { cas=1; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&m); cnt=1; CLEAR(id,0); REP(i,n) { scanf("%s",s[i]); REP(j,m) if(s[i][j]=='*') id[i][j]=cnt++;//编号 } CLEAR(mp,0); REP(i,n) { REP(j,m) { if(id[i][j]) { REP(k,4) { int xx=i+dr[k][0]; int yy=j+dr[k][1]; if(ok(xx,yy)&&id[xx][yy]) mp[id[i][j]][id[xx][yy]]=1; } } } } work(); } return 0; }
标签:
原文地址:http://blog.csdn.net/u013582254/article/details/43469619