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

 

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

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

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

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<string>
#include<vector>
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=;
char mp[MAXN][MAXN];
int disx[]={,,,-};
int disy[]={,-,,};
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-){
flot=;
return;
}
for(int i=;i<;i++){
int nx=x+disx[i],ny=y+disy[i];
if(nx<||ny<||nx>=n||ny>=m)continue;
if(!isdigit(mp[nx][ny]))continue;
if(!isjs(mp[x+disx[i]/][y+disy[i]/]))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]/][y+disy[i]/]=='/'&&mp[nx][ny]=='')continue;
double t=js(cur,mp[x+disx[i]/][y+disy[i]/],mp[nx][ny]-'');
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=;i<n;i++)scanf("%s",mp[i]);
flot=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(isdigit(mp[i][j])){
int temp=mp[i][j]-'';
mp[i][j]='#';
dfs(i,j,temp);
mp[i][j]=temp+'';
}
}
}
// for(int i=0;i<n;i++)puts(mp[i]);
if(flot)puts("Possible");
else puts("Impossible");
}
return ;
}

最新文章

  1. CSS 选择器及各样式引用方式
  2. Cvim的安装与使用
  3. JavaScript 数组详解
  4. c#类库和可移值类库的区别
  5. ASP CDONTS.NEWMAIL组件发送电邮(附下载)
  6. MyEclipse2014中SVN的使用方法
  7. UIDatePicker的简单用法
  8. 关于delete和delete[]
  9. Http状态码完整说明
  10. Golang性能调优入门
  11. JSP生成word文件
  12. Java基础语法(一)&lt;注释,关键字,常量,变量,数据类型,标识符,数据类型转换&gt;
  13. shell脚本 sed工具
  14. php、apache、nginx、线程、进程
  15. javascript中的数据结构
  16. 102/107. Binary Tree Level Order Traversal/II
  17. Memcached+WebApi记录
  18. IAR EWARM 字体设置
  19. linux网络编程:splice函数和tee( )函数高效的零拷贝
  20. [synergy]两台机器公用键盘鼠标

热门文章

  1. FVANCOP/ChartNew.js
  2. tr转换或删除字符
  3. java学习笔记day04
  4. 【Java基础】setter与getter方法
  5. videojs 视频开发API
  6. ASP.NET 开发框架汇总
  7. Spring 注入数据源
  8. session进程和服务
  9. linux学习笔记之硬盘分区
  10. 网络受限下,使用Nexus要解决的两个问题