hdu 5612 /BC 69C Baby Ming and Matrix games

6 篇文章 0 订阅

dfs暴搜即可 注意起点和终点任意 且 中间不能除0 

中间的保存值直接用double存的 就水过了=。= 可以优化成分数形式 就不会有精度问题(不过就15个数 也很难有精度问题)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#define scnaf scanf
#define cahr char
#define bug puts("bugbugbug");
using namespace std;
typedef long long ll;
const int mod=1000000007;
const int maxn=50+5;
const int inf=1e9;
const double eps=1e-8;
char a[maxn][maxn];
int vis[maxn][maxn];
int n,m;
double sum;
bool dfs(int x,int y,double v);
bool check(int x1,int y1,int x2,int y2,double v)
{
    if(1<=x2&&x2<=n&&1<=y2&&y2<=m);
    else return 0;
   // cout<<x2<<"====="<<y2<<endl;
    if(vis[x2][y2]) return 0;
    if(a[x1][y1]=='/'&&a[x2][y2]=='0') return 0;
    if(a[x1][y1]=='+') v+=a[x2][y2]-'0';
    if(a[x1][y1]=='-') v-=a[x2][y2]-'0';
    if(a[x1][y1]=='*') v*=a[x2][y2]-'0';
    if(a[x1][y1]=='/') v/=a[x2][y2]-'0';
    return dfs(x2,y2,v);
}
bool dfs(int x,int y,double v)
{
    if(fabs(v-sum)<eps) return 1;
    vis[x][y]=1;
    //cout<<x<<"====="<<y<<endl;
    if(check(x,y+1,x,y+2,v)) return 1;
    if(check(x,y-1,x,y-2,v)) return 1;
    if(check(x+1,y,x+2,y,v)) return 1;
    if(check(x-1,y,x-2,y,v)) return 1;
    vis[x][y]=0;
    return 0;
}
bool ok()
{
    for(int i=1;i<=n;i+=2)
        for(int j=1;j<=m;j+=2){
        memset(vis,0,sizeof(vis));
        if(dfs(i,j,a[i][j]-'0')) return 1;
    }
    return 0;
}
int main()
{
    int T_T;
    scanf("%d",&T_T);
    while(T_T--){
        scanf("%d%d%lf",&n,&m,&sum);
        for(int i=1;i<=n;i++)
            scanf("%s",a[i]+1);
        if(ok()) puts("Possible");
        else puts("Impossible");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值