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
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.)
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
Print Impossible if it is impossible to find such an expressions.
3 3 24
1*1
+#*
2*8
1 1 1
1
3 3 3
1*0
/#*
2*6
Possible
Possible
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 ;
}
Baby Ming and Matrix games(dfs计算表达式)的更多相关文章
- hdu5612 Baby Ming and Matrix games (dfs加暴力)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- HDU 5612 Baby Ming and Matrix games(DFS)
题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...
- hdoj--5612--Baby Ming and Matrix games(dfs)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- hdu 5612 Baby Ming and Matrix games
Baby Ming and Matrix games 题意: 给一个矩形,两个0~9的数字之间隔一个数学运算符(‘+’,’-‘,’*’,’/’),其中’/’表示分数除,再给一个目标的值,问是否存在从一 ...
- hdu 5612 Baby Ming and Matrix games(dfs暴力)
Problem Description These few days, Baby Ming is addicted to playing a matrix game. Given a n∗m matr ...
- HDU 5614 Baby Ming and Matrix tree 树链剖分
题意: 给出一棵树,每个顶点上有个\(2 \times 2\)的矩阵,矩阵有两种操作: 顺时针旋转90°,花费是2 将一种矩阵替换为另一种矩阵,花费是10 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- Java集合框架练习-计算表达式的值
最近在看<算法>这本书,正好看到一个计算表达式的问题,于是就打算写一下,也正好熟悉一下Java集合框架的使用,大致测试了一下,没啥问题. import java.util.*; /* * ...
- 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 ...
- C# 字符串计算表达式
C# 字符串计算表达式 string str="4+4+2.1"; 要的效果: double sum=4+4+2.1: 方案一: 动态计算表达式: 1 public class ...
随机推荐
- UESTC_基爷与加法等式 2015 UESTC Training for Search Algorithm & String<Problem C>
C - 基爷与加法等式 Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Subm ...
- hdu 2202 最大三角形_凸包模板
题意:略 思路:直接套用凸包模板 #include <iostream> #include <cstdio> #include <cmath> #include & ...
- 04747_Java语言程序设计(一)_第2章_运算和语句
推荐使用f2 public class Aserver { public static void main(String args[]) { float f1 = (float) 12.345; fl ...
- MyEclipse通过JDBC连接MySQL数据库基本介绍
转载自:http://www.jb51.net/article/31876.htm 1.前提是MyEclipse已经能正常开发Java工程 2.安装MySQL 个人使用的是版本是 mysql-5.0. ...
- Unity 集成联通SDK
我相信Unity程序员都会遇到加入SDK的问题,我相信如果你不会android编程,我相信你的CPU当场计算过快而爆炸! 这里也写笔记希望能帮助大家 如果有讲错的地方,希望大家能回复并且提供答案! ...
- Python 单词字母顺序不变且所有倒排
翻出google測试project师的一道题目: 设计一个函数,不论什么语言都能够,实现下面功能: 一个句子,将句子中的单词所有倒排过来,但单词的字母顺序不变.eg. this is a real ...
- 巧用DISPLAY_AWR函数与dba_hist_sqlstat结合查询SQL语句在指定节点指定时间范围内的历史执行计划
1.问题 通过调用dbms_xplan包中DISPLAY_AWR函数(DBMS_XPLAN.DISPLAY_AWR)可以从AWR数据中查看到SQL语句的历史执行计划,但是,DISPLAY ...
- 简单的javascript实例二(随页面滚动广告效果)
方便以后copy 页面代码: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "htt ...
- UIView的clipsTobounds属性
这里的clip是修剪的意思,bounds是边界的意思,合起来就是:如果子视图的范围超出了父视图的边界,那么超出的部分就会被裁剪掉.该属性在实际工程中还是非常实用的,必须要了解清楚.
- 在Yii框架中使用PHPExcel
PHPExcel是一个比较好用的php读取excel文件的类库,今天遇到了在yii中如何加载PHPExcel类文件的问题,因为Yii的autoload机制是安装类名去找文件,即文件名就是相应的类名,而 ...