(Split + minimum path overlay) Bzoj 2150

Source: Internet
Author: User

2150: Tribal war time limit:10 Sec Memory limit:259 MB
submit:518 solved:298
[Submit] [Status] [Discuss] Description

The tribes of Lanzerb in the upper part of country A, they were dissatisfied with the freezing environment, so they were prepared to go to the lower part of country A to gain greater territory. Country A is a m*n matrix, some of which are towns, some of which are Alpine Shen Jian uninhabited. Lanzerb his tribe into several armies, they agreed: 1. Each army can start from any town and can only go from the top to the bottom and not back. On the way only through town, not through the Alpine Shen Jian. 2. If a town is visited by an army, no other army can go to that town. 3. Every army can stop fighting in any town. 4. All the troops were strange, and they went a bit like a horse in chess. But the horse can only walk 1*2 route, and they can only go r*c route. Lanzerb's ambition made his goal to unify the country, but the force limit made them powerless in staffing. Assuming that each of them can successfully occupy all the towns that the army passes through, please help lanzerb calculate at least how many troops are needed to accomplish the reunification of the whole nation.

Input

The first line contains 4 integers m, N, R, C, meaning the description of the problem. The next M row is a string of length n for each row. If a character is '. ', it means that the place is a town; if the character is ' X ', it means the place is Alpine Shen Jian.

Output

Outputs an integer that represents the minimum number of armies.

Sample Input"Sample Input One"
3 3 1 2
...
. x.
...
"Sample input Two"
5 4 1 1
....
.. X.
... x
....
X...
Sample Output"Sample Output One"
4

"Sample Output Two"
5
"Sample description"

"Data Range"
100% of the data, 1<=m,n<=50,1<=r,c<=10.
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath > #include <cstdlib> #include <algorithm> #include <vector> #include <set> #include <stack > #include <map>using namespace std;vector<int> e[5001];int n,m,r,c,mp[51][51],lnk[5001],mark[5001];        Char s[51][51];bool dfs (int x) {for (int i=0;i<e[x].size (); i++) {int v=e[x][i];            if (mark[v]==-1) {mark[v]=1; if (lnk[v]==-1| |                DFS (Lnk[v])) {lnk[v]=x;            return true; }}} return false;}    int main () {int cnt=0;    memset (Lnk,-1,sizeof (LNK));    scanf ("%d%d%d%d", &n,&m,&r,&c);    for (int i=0;i<n;i++) scanf ("%s", S[i]);                for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {if (s[i][j]== '. ')        mp[i][j]=++cnt;    }} for (int i=0;i<n;i++) {for (int j=0;j<m;j++)    {if (Mp[i][j]) {if (I+r<n&&j+c<m&&mp[i+r][j+c])                {E[mp[i][j]].push_back (mp[i+r][j+c]+cnt); } if (I+c<n&&j+r<m&&mp[i+c][j+r]) {E[mp[i][j]].push                _back (MP[I+C][J+R]+CNT); } if (I+r<n&&j-c>=0&&mp[i+r][j-c]) {E[mp[i][j]].pus                H_back (MP[I+R][J-C]+CNT); } if (I+c<n&&j-r>=0&&mp[i+c][j-r]) {E[mp[i][j]].pus                H_back (MP[I+C][J-R]+CNT);    }}}} int ans=0;        for (int i=1;i<=cnt;i++) {memset (mark,-1,sizeof (Mark));    if (Dfs (i)) ans++;    } printf ("%d\n", Cnt-ans); return 0;}

  

(Split + minimum path overlay) Bzoj 2150

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.