博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Baby Ming and Matrix games(dfs计算表达式)
阅读量:6757 次
发布时间:2019-06-26

本文共 3183 字,大约阅读时间需要 10 分钟。

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

题解:让dfs表达式计算看能不能得到sum,必须是紧挨着的运算;

很好的一道搜索题,本来自己写了半天,各种考虑啊,然并卵,看了大神的解释,直接每次走两步就好了,那就简单多了,写了下就过了,Impossible i没大写错了一次;

发现dfs有时候真的很需要找对方法,否则麻烦还容易错;由于有除法运算,所以有误差,1e-8判断下误差;还有就是除以0的情况也考虑下;

代码:

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;const int INF=0x3f3f3f3f;#define SI(x) scanf("%d",&x)#define PI(x) printf("%d",x)#define P_ printf(" ")#define mem(x,y) memset(x,y,sizeof(x))typedef __int64 LL;const int MAXN=20;char mp[MAXN][MAXN];int disx[4]={ 0,0,2,-2};int disy[4]={ 2,-2,0,0};double sum;int flot;int n,m;bool isjs(char a){ if(a=='+'||a=='-'||a=='*'||a=='/')return true; else return false;}double js(double a,char x,double b){ switch(x){ case '+':return a+b;break; case '-':return a-b;break; case '*':return a*b;break; case '/':return a/b;break; }}void dfs(int x,int y,double cur){ if(flot)return; if(abs(cur-sum)<=1e-8){ flot=1; return; } for(int i=0;i<4;i++){ int nx=x+disx[i],ny=y+disy[i]; if(nx<0||ny<0||nx>=n||ny>=m)continue; if(!isdigit(mp[nx][ny]))continue; if(!isjs(mp[x+disx[i]/2][y+disy[i]/2]))continue; char temp=mp[nx][ny]; // printf("%I64d %c %c ",cur,mp[x+disx[i]/2][y+disy[i]/2],mp[nx][ny]); // printf("%I64d\n",js(cur,mp[x+disx[i]/2][y+disy[i]/2],mp[nx][ny]-'0')); if(mp[x+disx[i]/2][y+disy[i]/2]=='/'&&mp[nx][ny]=='0')continue; double t=js(cur,mp[x+disx[i]/2][y+disy[i]/2],mp[nx][ny]-'0'); mp[nx][ny]='#'; dfs(nx,ny,t); mp[nx][ny]=temp; }}int main(){ int T; SI(T); while(T--){ scanf("%d%d%lf",&n,&m,&sum); for(int i=0;i

 

转载地址:http://nsweo.baihongyu.com/

你可能感兴趣的文章
设计模式——观察者模式
查看>>
Python多线程 简明例子
查看>>
《地球上的星星》
查看>>
mysql数据库的主从同步,实现读写分离
查看>>
89 fcanf和fprintf
查看>>
javascript——自定义右键菜单
查看>>
求二叉树中相差最大的两个节点间的差值绝对值
查看>>
PHP 类名::class含义
查看>>
设计模式简介和分类,重点在总结
查看>>
数据库默认端口
查看>>
前端框架的区别,优缺点。
查看>>
oracle中使用sql语句创建表空间、用户、授权及使用命令导入导出
查看>>
layout中加载gif图片
查看>>
::符号
查看>>
“零甲醛”真的无污染?美博士环保开展调研
查看>>
unity博客 推荐(不断补充)
查看>>
图形处理的一些知识
查看>>
XPath
查看>>
[转]Shell脚本中获取SELECT结果值的方法
查看>>
No.2----数据类型(常用的)
查看>>