hdu5612 Baby Ming and Matrix games (dfs加暴力)

Baby Ming and Matrix games

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 849    Accepted Submission(s): 211

Problem Description

These few days, Baby Ming is addicted to playing a matrix game.

Given a n∗m matrix, the character in the matrix(i∗2,j∗2) (i,j=0,1,2...) are the numbers between 0−9. 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)

1≤T≤1000

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

题意:在这个矩阵内是否可以找到一个表达式的值等于sum;

思路:dfs找到所有的表达式,暴力一发;

AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
double sum;
int t,n,m,flag;
int vis[100][100],dir[4][2]={-2,0,2,0,0,2,0,-2};
double num[100][100];
char s[100][100];
int dfs(int x,int y,double ans)
{
    vis[x][y]=1;
    if(fabs(ans-sum)<=0.000000001)flag=1;
    for(int i=0;i<4;i++)
    {
       int fx=x+dir[i][0],fy=y+dir[i][1];
       int px=x+dir[i][0]/2,py=y+dir[i][1]/2;
       if(fx>=1&&fx<=n&&fy<=m&&fy>=1&&vis[fx][fy]==0&&s[fx][fy]!=‘#‘)
       {
           if(s[px][py]==‘+‘)
           dfs(fx,fy,ans+num[fx][fy]);
           else if(s[px][py]==‘*‘)
            dfs(fx,fy,ans*num[fx][fy]);
           else if(s[px][py]==‘-‘)
            dfs(fx,fy,ans-num[fx][fy]);
           else if(s[px][py]==‘/‘&&num[fx][fy]!=0)
            dfs(fx,fy,ans/num[fx][fy]);
       }
    }
    vis[x][y]=0;
    return 0;
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        flag=0;
        memset(vis,0,sizeof(vis));
        memset(num,-1,sizeof(num));
        scanf("%d%d%lf",&n,&m,&sum);
        for(int i=1;i<=n;i++)
        {
            scanf("%s",s[i]+1);
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]>=‘0‘&&s[i][j]<=‘9‘)
                {
                    num[i][j]=s[i][j]-‘0‘;
                }
            }
        }
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(s[i][j]>=‘0‘&&s[i][j]<=‘9‘)
                {
                    dfs(i,j,num[i][j]);
                }
            }
        }
        if(flag)printf("Possible\n");
        else printf("Impossible\n");
    }
    return 0;
}
时间: 2024-10-22 01:52:12

hdu5612 Baby Ming and Matrix games (dfs加暴力)的相关文章

Baby Ming and Matrix games(dfs计算表达式)

Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1210    Accepted Submission(s): 316 Problem Description These few days, Baby Ming is addicted to playing a matrix game

HDU 5612 Baby Ming and Matrix games

暴力搜索,据说精度卡的紧...但我是double过了的. #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<list> #include<algorithm> using namespace std; const double eps=1e-8; int dir[4][2],t[4][2]

BestCoder Round #69 (div.2) Baby Ming and Weight lifting(hdu 5610)

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 681    Accepted Submission(s): 280 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the

HDU 5610 Baby Ming and Weight lifting

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1365    Accepted Submission(s): 500 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(th

HDU 5610 Baby Ming and Weight lifting 暴力

Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(the weight of which can be ignored) and two different kinds of barbell disks(the weight of which are respectively a and b), the amount of each one being infinite.Baby Ming

Learning in Two-Player Matrix Games

3.2 Nash Equilibria in Two-Player Matrix Games For a two-player matrix game, we can set up a matrix with each element containing a reward for each joint action pair. Then the reward function for player becomes a matrix. A two-player matrix game is ca

HDU 5610 Baby Ming and Weight lifting(枚举)

Baby Ming and Weight lifting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 1439    Accepted Submission(s): 525 Problem Description Baby Ming is fond of weight lifting. He has a barbell pole(t

Codeforces --- 982C Cut &#39;em all! DFS加贪心

题目链接: https://cn.vjudge.net/problem/1576783/origin 输入输出: ExamplesinputCopy42 44 13 1outputCopy1inputCopy31 21 3outputCopy-1inputCopy107 18 48 104 76 59 33 52 102 5outputCopy4inputCopy21 2outputCopy0NoteIn the first example you can remove the edge bet

HDU 1728 逃离迷宫 单方向BFS 或者DFS加剪枝

逃离迷宫 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18519    Accepted Submission(s): 4463 Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有些地