题目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 ...
随机推荐
- [mysql] mysql_connect(): The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead in
From: http://www.ttlsa.com/php/deprecated-mysql-connect/ php 5个版本,5.2.5.3.5.4.5.5,怕跟不上时代,新的服务器直接上5.5 ...
- u3d 加载PNG做 UI图片
using UnityEngine; using System.Collections; using System.IO; using UnityEngine.UI; public class UIT ...
- Css 去除浮动
清除浮动的方法 清除浮动方法大致有两类,一类是clear:both | left | right ,另一类则是创建BFC,细分又可以分为多种. 通过在浮动元素末尾添加一个空的标签例如并设置样式为cle ...
- Javascript实例教程:querySelector()方法接受一个CSS查询并返回匹配模式的第一个子孙元素,如果没有匹配的元素则返回null。
文章简介:querySelector()方法接受一个CSS查询并返回匹配模式的第一个子孙元素,如果没有匹配的元素则返回null. querySelector()方法接受一个CSS查询并返回匹配模式的第 ...
- Java编程思想学习笔记——访问权限修饰词
几种访问权限修饰词 public,protected,private,friendly(Java中并无该修饰词,即包访问权限,不提供任何访问修饰词) 使用时,放置在类中成员(域或方法)的定义之前的,仅 ...
- CorelDRAW中如何再制对象详解
再制对象指的是快捷地将对象按一定的方式复制为多个对象,此种复制是复制的复制,再制不仅可以节省复制的时间,再制间距还可以保证复制效果.本教程将详解如何在CorelDRAW软件中再制对象. CorelDR ...
- QT OpenGL绘制三维图形(立方体、圆柱体、圆锥、球体、圆环等等)
本文使用QGLWidget来绘制各种三维基本图形,包括立方体.圆柱体.圆锥.球体.圆环等等,涉及包括基本绘制以及上色.纹理.旋转等操作. 使用的软件版本:QT5.12 + QT Creater4.8. ...
- oracle 产生随机数
-- 产生一个任意大小的随机数select dbms_random.random from dual; -- 产生一个100以内的随机数select abs(mod(dbms_random.rando ...
- liunx 时间ntp同步服务器
1.root 用户下安装 yum install ntp -y 报错如下: 29 Apr 00:25:04 ntpdate[8786]: the NTP socket is in use, exiti ...
- linq to xml 增删查改
一.XML基本概述 XML文件是一种常用的文件格式,例如WinForm里面的app.config以及Web程序中的web.config文件,还有许多重要的场所都有它的身影.Xml是Internet环境 ...