hdu 5067(暴力搜索)

Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 852    Accepted Submission(s): 348

Problem Description

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.(1≤n,m≤50).
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.( 0≤a[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的矩阵里面有k个石头(k<=10) 哈利波特从(0,0)点出发,可以沿着x轴或者y轴在矩阵里面行走,他的任务是在最短的时间内将石头全部取完,问至少需要多少时间他才能取完所有的石头然后回到原点??

题解:构图然后暴搜。。剪枝。。

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
using namespace std;
const int INF = 999999999;
struct Point
{
    int x,y;
} p[15];
int n,m,res,cnt;
int graph[15][15];
bool vis[15];
void dfs(int x,int dept,int ans)
{
    vis[x] = true;
    if(ans>res) return ; ///剪枝
    if(dept==cnt-1)
    {
        ans += graph[x][0]; ///走回去
        res = min(res,ans);
        return ;
    }
    for(int i=0; i<cnt; i++)
    {
        if(!vis[i]&&graph[x][i]!=INF&&x!=i)
        {
            dfs(i,dept+1,ans+graph[x][i]);
            vis[i] = false;
        }
    }
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt = 0;
        int v;
        for(int i=0; i<n; i++)
        {
            for(int j=0; j<m; j++)
            {
                scanf("%d",&v);
                if(i==0&&j==0)
                {
                    p[cnt].x = i,p[cnt++].y=j;
                }
                else if(v)
                {
                    p[cnt].x = i,p[cnt++].y=j;
                }
            }
        }
        for(int i=0; i<cnt; i++)
        {
            for(int j=0; j<cnt; j++)
            {
                if(i==j)graph[i][j] = 0;
                else graph[i][j] =INF;
            }
        }
        for(int i=0; i<cnt; i++)
        {
            for(int j=i+1; j<cnt; j++)
            {
                graph[i][j] = graph[j][i] = abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y);
            }
        }
        /*for(int i=0; i<cnt; i++)
        {
            for(int j=0; j<cnt; j++)
            {
                printf("%d ",graph[i][j]);
            }
            printf("\n");
        }*/
        memset(vis,false,sizeof(vis));
        res = INF;
        vis[0] = true;
        dfs(0,0,0);
        printf("%d\n",res);
    }
    return 0;
}
时间: 2024-10-21 10:15:05

hdu 5067(暴力搜索)的相关文章

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

hdu 1399 Starship Hakodate-maru (暴力搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1399 题目大意:找到满足i*i*i+j*(j+1)*(j+2)/6形式且小于等于n的最大值. 1 #include<iostream> 2 #include<cstdio> 3 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 while(scanf("%d",&n),n) 10 { 11 int j,

HDU 3131 One…Two…Five! (暴力搜索)

题目链接:HDU 3131 One-Two-Five! (暴力搜索) 题意:给出一串数字,要求用加,减,乘,除(5/2=2)连接(计算无优先级:5+3*6=8*6=48),求所有结果中,含有'3'且该数字出现频率最大,若频率相等,输出数字最大的. 暴力解决之 AC代码: #include <stdio.h> #include <map> #include <string.h> #include <algorithm> #define LL __int64 u

hdu 1427 速算24点 dfs暴力搜索

速算24点 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description 速算24点相信绝大多数人都玩过.就是随机给你四张牌,包括A(1),2,3,4,5,6,7,8,9,10,J(11),Q(12),K(13).要求只用'+','-','*','/'运算符以及括号改变运算顺序,使得最终运算结果为24(每个数必须且仅能用一次).游戏很简单,但遇到无解的

ACM 暴力搜索题 题目整理

UVa 129 Krypton Factor 注意输出格式,比较坑爹. 每次要进行处理去掉容易的串,统计困难串的个数. #include<iostream> #include<vector> #include<cmath> #include<map> #include<algorithm> #include<cstring> #include<cstdio> #include<cstdlib> #include

[暴力搜索] POJ 3087 Shuffle&#39;m Up

Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10003   Accepted: 4631 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting with two stacks o

poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一个边界外,跳到了另一边的边界外,每跳一次对那个点进行标记. 现在给你很多青蛙跳过后的所标记的所有点,那请你从这些点里面找出 一条可能的路径里面出现过的标记点最多. 分析:先排序(目的是方便剪枝,break),然后枚举两个点,这两个 点代表这条路径的起始的两个点.然后是三个剪枝,下面有. 开始遍历时,

HDU 5067 Harry And Dig Machine(状压dp)

HDU 5067 Harry And Dig Machine 思路:由于点才10个,在加上一个起点,处理出每个点之间的曼哈顿距离,然后用状压dp搞,状态表示为: dp[i][s],表示在i位置,走过的点集合为s的最小代价 代码: #include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> using namespace std; const int N = 15;