题目1091:棋盘游戏(DFS)
题目链接:http://ac.jobdu.com/problem.php?pid=1091
详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus
参考代码:
//
// 1091 棋盘游戏.cpp
// Jobdu
//
// Created by PengFei_Zheng on 04/05/2017.
// Copyright © 2017 PengFei_Zheng. All rights reserved.
// #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#include <climits>
#include <vector>
#define MAX_SIZE 6
//#define debug using namespace std; int n, ans = INT_MAX;
int xStart,yStart,xEnd,yEnd; int map[MAX_SIZE][MAX_SIZE];
bool visited[MAX_SIZE][MAX_SIZE]; int change[][]={{-,,,},{,,-,}}; void DFS(int x, int y, int status, int sum){
int nextX,nextY,cost;
if(sum < ans){
if(x==xEnd && y==yEnd){
ans = sum;
return ;
}
for(int i = ; i < ; i ++){
nextX = x + change[][i];
nextY = y + change[][i];
if(!visited[nextX][nextY] && nextX>= && nextX<MAX_SIZE && nextY>= && nextY<MAX_SIZE){
cost = map[nextX][nextY]*status;
visited[nextX][nextY]=true;
DFS(nextX,nextY,cost%+,sum+cost);//注意参数传递
visited[nextX][nextY]=false;
}
}
}
}
int main(){
#ifdef debug
freopen("/Users/pengfei_zheng/Desktop/input.txt", "r", stdin);
#endif
scanf("%d",&n);
while(n--){
memset(map,,sizeof(map));
for(int i = ; i < MAX_SIZE ; i++){
for(int j = ; j < MAX_SIZE ; j++){
scanf("%d",&map[i][j]);
visited[i][j]=false;
}
}
ans = INT_MAX;
scanf("%d %d %d %d",&xStart,&yStart,&xEnd,&yEnd);
DFS(xStart,yStart,,);
printf("%d\n",ans);
}
return ;
}
/**************************************************************
Problem: 1091
User: zpfbuaa
Language: C++
Result: Accepted
Time:10 ms
Memory:1520 kb
****************************************************************/
题目1091:棋盘游戏(DFS)的更多相关文章
- 九度oj 题目1091:棋盘游戏
题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径: 1.只能沿上下左右四个方向移动 2.总代价是没走一步的 ...
- Codeforces Round #381 (Div. 2)A. Alyona and copybooks(dfs)
A. Alyona and copybooks Problem Description: Little girl Alyona is in a shop to buy some copybooks f ...
- [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】
题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...
- UVA - 11882 Biggest Number(dfs+bfs+强剪枝)
题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...
- HDU 2586 How far away(dfs+邻接表)
How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...
- [LeetCode] 339. Nested List Weight Sum_Easy tag:DFS
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...
- [LeetCode] 329. Longest Increasing Path in a Matrix_Hard tag: Dynamic Programming, DFS, Memoization
Given an integer matrix, find the length of the longest increasing path. From each cell, you can eit ...
- poj3279(dfs+二进制枚举思路)
题意转载自https://www.cnblogs.com/blumia/p/poj3279.html 题目属性:DFS 相关题目:poj3276 题目原文:[desc]Farmer John know ...
随机推荐
- jquery选择器玩得不6啊,只能慢慢写判断了,唉..........................
jquery选择器玩得不6啊,只能慢慢写判断了,唉..........................
- OpenGL中各种坐标系的理解
转载:https://blog.csdn.net/meegomeego/article/details/8686816 OPENGL坐标系可分为:世界坐标系和当前绘图坐标系. 世界坐标系以屏幕中心为原 ...
- JVM知识点总览-中高级Java工程师面试必备
对于搞开发的我们其实也是一样,现在流行的框架越来越多,封装的也越来越完善,各种框架可以搞定一切,几乎不用关注底层的实现,初级程序员只要熟悉基本的使用方法,便可以快速的开发上线:但对于高级程序员来讲,内 ...
- Yslow-23条军规
YslowYahoo发布的一款基于FireFox的插件,主要是为了提高网页性能而设计的,下面是它提倡了23条规则,还是很不错的,分享一下: 1.减少HTTP请求次数 合并图片.CSS.JS,改进首次访 ...
- jsp新建项目
1.在原有项目的基础上新建一个文件夹 在文件夹内新建一个jsp文件 取名 JSP容器处理JSP文件需要以下三个阶段:翻译——编译——执行 JSP的页面元素包括 静态内容-HTML静态文本 指令-以“& ...
- python单例模式控制成只初始化一次,常规型的python单例模式在新式类和经典类中的区别。
单例模式的写法非常多,但常规型的单例模式就是这样写的,各种代码可能略有差异,但核心就是要搞清楚类属性 实例属性,就很容易写出来,原理完全一模一样. 如下: 源码: class A(object): d ...
- python内存泄漏,python垃圾手动回收,1
部署的舆情系统,内存变大,找原因. 一个小例子. def func(): local_list = list(range(10000000)) func() time.sleep(200) 能够观察到 ...
- 使用Matplotlib画图系列(一)
实现一个最简单的plot函数调用: import matplotlib.pyplot as plt y=pp.DS.Transac_open # 设置y轴数据,以数组形式提供 x=len(y) # 设 ...
- Android安装器学习笔记(一)
Android安装器学习笔记(一) 一.Android应用的四种安装方式: 1.通过系统应用PackageInstaller.apk进行安装,安装过程中会让用户确认 2.系统程序安装:在开机的时候自动 ...
- POJ1159——Palindrome
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 53647 Accepted: 18522 Desc ...