hdu 5067 Harry And Dig Machine

 As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm. 
  Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
 

Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m.(1n,m50).
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0a[i][j]100 , and no more than 10 of a[i][j] will be positive integer).
 

Output
For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.
 

Sample Input
3 3 0 0 0 0 100 0 0 0 0 2 2 1 1 1 1
 

Sample Output
4 4
 
題意:給你一個n*m得圖,每個點有一定數量的石子,問你將石子轉移到(0.0)點所需最最少時間,每次轉移聯不限,相鄰對的轉移時間爲1;
  
題解:由題目分析可知,就是求經過每個石堆一次最後回到起點的最小時間,由於石子最多有10堆,處理出圖後我們便可使用狀壓的方法來求解,dp[s][i]表是當前狀態下的終點在j點, 因此不難得出轉移方程dp[s][j]=min(dp[s][j],dp[]s^(1<<j)[i]+map[i][j])

代碼:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define inf 9999999




int dp[(1<<11)+5][15];
int ma[15][15];
struct node
{
    int x,y;
}e[15];
int main()
{
    int n,m,xx;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int k=1;
        e[0].x=0;
        e[0].y=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                scanf("%d",&xx);
                if(i==0&&j==0)continue;
                if(xx!=0)
                {
                    e[k].x=i;
                    e[k].y=j;
                    k++;
                }
            }
        }
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<k;j++)
            {
                if(i!=j)
                ma[i][j]=inf;
            }
        }
        for(int i=0;i<k;i++)
        {
            for(int j=0;j<k;j++)
            {
                if(i==j)continue;
                if(ma[i][j]>(abs(e[i].x-e[j].x)+abs(e[i].y-e[j].y)))
                {
                    ma[i][j]=ma[j][i]=abs(e[i].x-e[j].x)+abs(e[i].y-e[j].y);
                }
            }
        }
        for(int s=0;s<(1<<k);s++)
        {
            for(int j=0;j<k;j++)
            {
                dp[s][j]=inf;
            }
        }
        for(int s=0;s<(1<<k);s++)
        {
            for(int now=0;now<k;now++)
            {
                if(!(s&(1<<now)))continue;
                if((1<<now)==s)
                {
                    dp[s][now]=ma[0][now];//當此轉貸s中只含一個點時,一定是從0點到詞此點
                    continue;
                }
                for(int i=0;i<k;i++)
                {
                    if(i==now)continue;
                    if(!(s&(1<<i)))continue;
                    int snow=(1<<now)^s;
                    if(dp[snow][i]==inf)continue;
                    dp[s][now]=min(dp[snow][i]+ma[i][now],dp[s][now]);
                }
            }
        }
        int ans=inf;
        for(int i=0;i<k;i++)
            ans=min(dp[(1<<k)-1][i]+ma[0][i],ans);
        if(ans!=inf)
        printf("%d\n",ans);
        else
        printf("0\n");
    }
    return 0;
}


發佈了34 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章