POJ 3133 Manhattan Wiring

Source: Internet
Author: User
Tags min pow printf time limit

Manhattan Wiring

Time limit:5000ms Memory limit:65536k
Total submissions:1867 accepted:1100

Description

There is a rectangular area containing nxm cells. Both cells is marked with "2", and another, and "3". Some cells is occupied by obstacles. You should connect the both "2" s and also the "3" s with non-intersecting lines. Lines can run only vertically or horizontally connecting centers of cells without obstacles.

Lines cannot run on a cell with an obstacle. Only one line can run in a cell at the most once. Hence, a line cannot intersect and the other line, nor with itself. Under these constraints, the total length of the lines should is minimized. The length of a line is defined as the number of cell borders it passes. In particular, a line connecting cells sharing their border have length 1.

Fig. 1 (a) shows an example setting. Fig. 1 (b) shows-lines satisfying the constraints above with minimum total length 18.

Figure 1:an Example of setting and its solution

Input

The input consists of multiple datasets, each in the following format.

Nm
Row1
...
Rown

n is the number of rows which satisfies 2≤n≤9. M is the number of columns which satisfies 2≤m≤9. Each rowi are a sequence of m digits separated by a space. The digits mean the following.

0:empty

1:occupied by an obstacle

2:marked with "2"

3:marked with "3"

The end of the input is indicated with a line containing, zeros separated by a space.

Output

For each dataset, one line containing the minimum total length of the lines should is output. If There is no pair of lines satisfying the requirement, answer "0" instead. No other characters should is contained in the output.

Analysis
Test instructions: Give you a n*m grid, each grid is a number in the 0,1,2,3, a total of 2 2 and 2 3, the two disjoint lines will be 2 2 and 2 3 to connect together the minimum length of the line required. 0 delegates can walk, 1 means this point is not allowed to go.

The sum of God's questions ... Adjusted the day, and finally came home on the way to come up with their own fault where 2333 ...

This is a basic plug DP bar ... The state is represented by a 3 binary string, from the top line of the current grid to the left line of the current grid. 1 means that the edge is connected to 2 of the line, 2 means that the line is connected by 3 ... Then there is the endless state shift ... We must pay attention to the conditions of transfer.

I feel that this problem than the slope optimization DP also TMD difficult ...

Not me, I wrote the code should be the shortest in the whole csdn (laughter)

Code

POJ 3133 start dead #include <iostream> #include <cstring> #include <cstdio> #define P (i,j) (I/pow[j])%3 #
Define FO (i,j,k) for (i=j;i<=k;i++) #define MOVE (k) (K-p (k,0)) *3%pow[m+1]//equivalent to move left one using namespace std;
int N,m,inf,now,ans;
int pow[15],a[15][15],dp[2][177148];
    inline void dynamic () {int i,j,p,q,k,ttt=inf;
    now=0;
    dp[now][0]=0;
          Fo (i,1,n) fo (j,1,m) {now^=1;
          memset (dp[now],0x3f,sizeof Dp[now]);
              FO (k,0,pow[m+1]-1) if (dp[now^1][k]!=inf)//TM State Transfer {BOOL flag=0;  int left=p (k,0), up=p (k,m); Status of left line & top line status if (a[i][j]!=2 && a[i][j]!=3 &&!left &&!up) d P[now][move (k)]=min (Dp[now][move (k)],dp[now^1][k]);
              Do not put if (a[i][j]==1) continue;
                  if (a[i][j]==2 | | a[i][j]==3) {int tmp=a[i][j]-1; if (left==tmp &&!up && j>1) dp[now][mOve (k)]=min (Dp[now][move (k)],dp[now^1][k]);
                  if (up==tmp &&!left && i>1) dp[now][move (k)]=min (Dp[now][move (k)],dp[now^1][k]); if (!left &&!up) {if (i<n) Dp[now][move (k) +tmp*3]=min (Dp[now][move (k) +t  Mp*3],dp[now^1][k]);  Under short term if (j<m) Dp[now][move (k) +tmp]=min (Dp[now][move (k) +tmp],dp[now^1][k]);           
              Right short term} flag=1;
              } if (flag) continue; if (!left &&!up && i<n && j<m)//left 0 on 0 {dp[now][move (k) +4]
                  =min (Dp[now][move (k) +4],dp[now^1][k]+1);
              Dp[now][move (k) +8]=min (Dp[now][move (k) +8],dp[now^1][k]+1); } if (!left && up==1 && i>1)//left 0 on 1 {if (i<n) Dp[now    ][move (k) +3]=min (Dp[now][move (k) +3],dp[now^1][k]+1);      
     Vertical             if (j<m) Dp[now][move (k) +1]=min (Dp[now][move (k) +1],dp[now^1][k]+1); } if (!left && up==2 && i>1)//left 0 on 2 {if (i<n) Dp[now    ][move (k) +6]=min (Dp[now][move (k) +6],dp[now^1][k]+1);
              Vertical if (j<m) Dp[now][move (k) +2]=min (Dp[now][move (k) +2],dp[now^1][k]+1); } if (left==1 &&!up && j>1)//left 1 on 0 {if (j<m) dp[now][  Move (k) +1]=min (Dp[now][move (k) +1],dp[now^1][k]+1);  Horizontal if (i<n) Dp[now][move (k) +3]=min (Dp[now][move (k) +3],dp[now^1][k]+1); 
              Fold down} if (left==1 && up==1 && i>1 && j>1)//left 1 on 1
              {Dp[now][move (k)]=min (Dp[now][move (k)],dp[now^1][k]+1); if (left==2 &&!up && j>1)//left 2 on 0 {if (j<m) Dp[now] [Move (k) +2]=min (Dp[now][move (k) +2],dp[now^1][k]+1); 
              if (i<n) Dp[now][move (k) +6]=min (Dp[now][move (k) +6],dp[now^1][k]+1);
                  } if (left==2 && up==2 && i>1 && j>1)//left 2 on 2 { 
              Dp[now][move (k)]=min (Dp[now][move (k)],dp[now^1][k]+1);
    }}}} int main () {int i,j,k;
    Pow[0]=1;
    Fo (i,1,11) pow[i]=pow[i-1]*3;
        while (scanf ("%d%d", &n,&m) && n && m) {memset (dp,0x3f,sizeof DP);
        ANS=INF=DP[0][0];
        Fo (i,1,n) fo (j,1,m) scanf ("%d", &a[i][j]);
        Dynamic ();
        Fo (k,0,pow[m+1]-1) ans=min (Ans,dp[now][k]);
        if (ans>=inf) printf ("0\n");
    else printf ("%d\n", ans+2);
} return 0;
 }

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.