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];
double a[100][100];
bool flag[100][100];
char s[100][100];
int n,m;
double sum;
bool ans;
double path[1000];

void init()
{
    dir[0][0]=2; dir[0][1]=0;
    dir[1][0]=-2; dir[1][1]=0;
    dir[2][0]=0; dir[2][1]=2;
    dir[3][0]=0; dir[3][1]=-2;

    t[0][0]=1; t[0][1]=0;
    t[1][0]=-1; t[1][1]=0;
    t[2][0]=0; t[2][1]=1;
    t[3][0]=0; t[3][1]=-1;

    for(int i=0;i<n;i++)
        for(int j=0;j<m;j++)
            a[i][j]=-1.0;

    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            if(s[i][j]>=‘0‘&&s[i][j]<=‘9‘)
                a[i][j]=1.0*(s[i][j]-‘0‘);

    memset(flag,0,sizeof flag);
}

void dfs(double nowNum,int nowx,int nowy,int tot)
{
    if(fabs(nowNum-sum)<eps)
    {
        ans=1;
        return;
    }

        for(int i=0; i<4; i++)
        {
            double newNum;
            int newX,newY;
            newX=nowx+dir[i][0];
            newY=nowy+dir[i][1];
            if(newX>=0&&newX<n)
            {
                if(newY>=0&&newY<m)
                {
                    if(flag[newX][newY]==0)
                    {
                        flag[newX][newY]=1;
                        if(s[nowx+t[i][0]][nowy+t[i][1]]==‘+‘)
                        {
                            newNum=nowNum+a[newX][newY];

                            dfs(newNum,newX,newY,tot+1);
                            if(ans) return;

                        }
                        else if(s[nowx+t[i][0]][nowy+t[i][1]]==‘-‘)
                        {
                            newNum=nowNum-a[newX][newY];

                            dfs(newNum,newX,newY,tot+1);
                            if(ans) return;

                        }
                        else if(s[nowx+t[i][0]][nowy+t[i][1]]==‘*‘)
                        {
                            newNum=nowNum*a[newX][newY];

                            dfs(newNum,newX,newY,tot+1);
                            if(ans) return;

                        }
                        else if(s[nowx+t[i][0]][nowy+t[i][1]]==‘/‘)
                        {
                            if(fabs(a[newX][newY]-0.0)<eps) {
                                    flag[newX][newY]=0;continue;
                            }
                            newNum=nowNum/a[newX][newY];

                            dfs(newNum,newX,newY,tot+1);
                            if(ans) return;

                        }
                        flag[newX][newY]=0;
                    }
                }
            }
        }
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%lf",&n,&m,&sum);

        for(int i=0; i<n; i++) scanf("%s",s[i]);
        init();

        ans=0;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                if(a[i][j]>=0)
                {
                    memset(flag,0,sizeof flag);
                    flag[i][j]=1;
                    dfs(a[i][j],i,j,1);
                    if(ans) break;
                }

        if(ans) printf("Possible\n");
        else printf("Impossible\n");
    }
    return 0;
}
时间: 2024-12-06 16:27:20

HDU 5612 Baby Ming and Matrix games的相关文章

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.

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 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

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

HDU 5611 Baby Ming and phone number

#include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<list> #include<algorithm> using namespace std; int T,n; char s[20]; long long a,b; int q1[20],q2[20]; long long f() { if

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 4349 Xiao Ming&#39;s Hope (Lucas)

题意:给定一个 n,问你在 C(n, 0) - C(n , n) 中有多少个奇数. 析:Lucas定理,C(b[i], a[i]),只要不为0,那么就是奇数,然后b[i],是固定的,也就是说a[i] 只有 b[i]+1种情况.最后乘起来就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun