HDU 5612 Baby Ming and Matrix games (搜索)

12 篇文章 0 订阅
12 篇文章 0 订阅

Baby Ming and Matrix games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 392    Accepted Submission(s): 82
Problem Description
These few days, Baby Ming is addicted to playing a matrix game.
Given a  nm  matrix, the character in the matrix (i2,j2) (i,j=0,1,2...)  are the numbers between  09 . There are an arithmetic sign (‘+’, ‘-‘, ‘ ’, ‘/’) between every two adjacent numbers, other places in the matrix fill with ‘#’.
The question is whether you can find an expressions from the matrix, in order to make the result of the expressions equal to the given integer  sum . (Expressions are calculated according to the order from left to right)
Get expressions by the following way: select a number as a starting point, and then selecting an adjacent digital X to make the expressions, and then, selecting the location of X for the next starting point. (The number in same place can’t be used twice.)
Input
In the first line contains a single positive integer  T , indicating number of test case.
In the second line there are two odd numbers  n,m , and an integer sum( 1018<sum<1018 , divisor 0 is not legitimate, division rules see example)
In the next  n  lines, each line input  m  characters, indicating the matrix. (The number of numbers in the matrix is less than  15 )
1T1000
Output
Print Possible if it is possible to find such an expressions.
Print Impossible if it is impossible to find such an expressions.
Sample Input
  
  
3 3 3 24 1*1 +#* 2*8 1 1 1 1 3 3 3 1*0 /#* 2*6
Sample Output
  
  
Possible Possible Possible
Hint
The first sample:1+2*8=24 The third sample:1/2*6=3
题意: 输入n,m,sum分别代表着矩阵的长,宽,要求的值,从矩阵的任何一个位置是0~9的数开始搜索,能否找到一个连续的计算式,使这个计算式的值为sum

方法:暴搜!!!!

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

#define LL long long
#define eps 1e-10

using namespace std;
double sum;
int n,m;
char str[50][50];
bool vis[55][55];
int Dir[][2] = {{0,1},{0,-1},{1,0},{-1,0}};

int DFS(int x, int y, double ant)
{

    if(fabs(ant - sum) <= eps)
        return 1;
    vis[x][y] = true;
    for(int i=0; i<4; i++)
    {
        int xx = x + Dir[i][0]*2;
        int yy = y + Dir[i][1]*2;
        int Deno = str[xx][yy] - '0';
        if(vis[xx][yy]|| xx < 0 || xx >= n || yy < 0 || yy >= m)
            continue;
        else if(str[x + Dir[i][0]][y + Dir[i][1]] == '+')
        {
            if(DFS(xx,yy,ant+Deno))
                return 1;
        }
        else if(str[x + Dir[i][0]][y + Dir[i][1]] == '-')
        {
            if(DFS(xx,yy,ant-Deno))
                return 1;
        }
        else if(str[x + Dir[i][0]][y + Dir[i][1]] == '*')
        {
            if(DFS(xx,yy,ant*Deno))
                return 1;
        }
        else if(str[x + Dir[i][0]][y + Dir[i][1]] == '/')
        {
            if(Deno)
            {
                if(DFS(xx,yy,ant/Deno))
                    return 1;
            }
        }
    }
    vis[x][y] = false;
    return 0;
}
int main()
{
    int T;
    scanf("%d",&T);
    while(T-- && scanf("%d%d%lf",&n,&m, &sum))
    {
        memset(vis, false, sizeof(vis));
        for(int i=0; i<n; i++)
            scanf("%s",str[i]);
        int flag = 0;
        for(int i=0; i<n&&!flag; i+=2)
            for(int j=0; j<m&&!flag; j+=2)
                flag |= DFS(i,j,str[i][j]-'0');

        if(flag)
            printf("Possible\n");
        else
            printf("Impossible\n");
    }


    return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值