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
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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
double sum;
int t,n,m,flag;
int vis[][],dir[][]={-,,,,,,,-};
double num[][];
char s[][];
int dfs(int x,int y,double ans)
{
vis[x][y]=;
if(fabs(ans-sum)<=0.000000001)flag=;
for(int i=;i<;i++)
{
int fx=x+dir[i][],fy=y+dir[i][];
int px=x+dir[i][]/,py=y+dir[i][]/;
if(fx>=&&fx<=n&&fy<=m&&fy>=&&vis[fx][fy]==&&s[fx][fy]!='#')
{
if(s[px][py]=='+')
dfs(fx,fy,ans+num[fx][fy]);
else if(s[px][py]=='*')
dfs(fx,fy,ans*num[fx][fy]);
else if(s[px][py]=='-')
dfs(fx,fy,ans-num[fx][fy]);
else if(s[px][py]=='/'&&num[fx][fy]!=)
dfs(fx,fy,ans/num[fx][fy]);
}
}
vis[x][y]=;
return;
}
int main()
{
scanf("%d",&t);
while(t--)
{
flag=;
memset(vis,,sizeof(vis));
memset(num,-,sizeof(num));
scanf("%d%d%lf",&n,&m,&sum);
for(int i=;i<=n;i++)
{
scanf("%s",s[i]+);
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(s[i][j]>='0'&&s[i][j]<='9')
{
num[i][j]=s[i][j]-'0';
}
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
if(s[i][j]>='0'&&s[i][j]<='9')
{
dfs(i,j,num[i][j]);
}
}
}
if(flag)printf("Possible\n");
else printf("Impossible\n");
}
return;
}
hdu5612 Baby Ming and Matrix games (dfs加暴力)的更多相关文章
- HDU 5612 Baby Ming and Matrix games(DFS)
题目链接 题解:题意为给出一个N*M的矩阵,然后(i∗2,j∗2) (i,j=0,1,2...)的点处是数字,两个数字之间是符号,其他位置是‘#’号. 但不知道是理解的问题还是题目描述的问题,数据中还 ...
- Baby Ming and Matrix games(dfs计算表达式)
Baby Ming and Matrix games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- 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 树上有一种操作,将一条路经上的所有矩阵都变为 ...
- 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 ...
- 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 ( ...
- hdu 5611 Baby Ming and phone number(模拟)
Problem Description Baby Ming collected lots of cell phone numbers, and he wants to sell them for mo ...
随机推荐
- 并行编程(2) - sum.msic.Unsafe 二
整理了几个曾经从网上记录sum.msic.Unsafe类的演示样例.供大家參考: package com.fish.unsafe; import java.io.File; import java.i ...
- python入门课程 第二章 安装Python
2-1 选择python版本首先python2.7和python3是不可以通用的目前丰富的类库都支持python2.7,所以选用Python2.7 选择python2.7版本2-2 window ...
- mysql慢查询日志分析工具(python写的)
D:\NormalSoftware>python mysql_filter_slow_log.py ./mysql1-slow.log --no-duplicates --sort-avg-qu ...
- LNMP环境搭建(三:PHP)
1.获取php源码 # cd /usr/local/src/ # wget http://cn2.php.net/get/php-7.0.15.tar.gz/from/this/mirror 2.解压 ...
- 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;
答案:一. int strcmp(char *source, char *dest) { /* assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stder ...
- Netty ByteBuf(图解之 2)| 秒懂
目录 Netty ByteBuf(图解二):API 图解 源码工程 写在前面 ByteBuf 的四个逻辑部分 ByteBuf 的三个指针 ByteBuf 的三组方法 ByteBuf 的引用计数 Byt ...
- flex (html弹性布局)
flex是什么? 任何容器都可以指定为flex布局: .box{ display: flex; /* 行内元素可以使用:inline-flex,webket内核浏览器必须 -webkit-flex ...
- Asp.Net 5 Web Hook
首先,然我们来看一下WebHooks是什么.WebHooks是一个协议.它们是HTTP回调技术.并且它们是"用户定义的HTTP回调".你和 (或) 您的应用程序在有什么事情发生时会 ...
- python+NLTK 自然语言学习处理七:N-gram标注
在上一章中介绍了用pos_tag进行词性标注.这一章将要介绍专门的标注器. 首先来看一元标注器,一元标注器利用一种简单的统计算法,对每个标识符分配最有可能的标记,建立一元标注器的技术称为训练. fro ...
- C#练习DataReader
SQL代码: create database ThreeDb go USE ThreeDb; GO CREATE TABLE classify --分类表 ( id ,), name ) not nu ...