题目链接: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)的更多相关文章

  1. 九度oj 题目1091:棋盘游戏

    题目描述: 有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径:    1.只能沿上下左右四个方向移动    2.总代价是没走一步的 ...

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

  3. [BZOJ 1082] [SCOI2005] 栅栏 【二分 + DFS验证(有效剪枝)】

    题目链接:BZOJ - 1082 题目分析 二分 + DFS验证. 二分到一个 mid ,验证能否选 mid 个根木棍,显然要选最小的 mid 根. 使用 DFS 验证,因为贪心地想一下,要尽量先用提 ...

  4. UVA - 11882 Biggest Number(dfs+bfs+强剪枝)

    题目大意:给出一个方格矩阵,矩阵中有数字0~9,任选一个格子为起点,将走过的数字连起来构成一个数,找出最大的那个数,每个格子只能走一次. 题目分析:DFS.剪枝方案:在当前的处境下,找出所有还能到达的 ...

  5. HDU 2586 How far away(dfs+邻接表)

    How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...

  6. [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. ...

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

  8. [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 ...

  9. poj3279(dfs+二进制枚举思路)

    题意转载自https://www.cnblogs.com/blumia/p/poj3279.html 题目属性:DFS 相关题目:poj3276 题目原文:[desc]Farmer John know ...

随机推荐

  1. MySQL的varchar长度问题

    From: http://blog.csdn.net/longyulu/article/details/7863737 http://dinglin.iteye.com/blog/914276 htt ...

  2. PHP和Apache结合 Apache默认虚拟主机

  3. js 历史

    原文http://javascript.ruanyifeng.com/introduction/history.html JavaScript的诞生 JavaScript 因为互联网而生,紧随着浏览器 ...

  4. Linux服务器svn与项目同步

    命令:svn checkout svn://192.168.67.131/trunk/w1

  5. Python中MongoDB使用

    MongoDB的层级为 database -->collection --> document 安装MongoDB,启动mongo服务 PyMongo模块是Python对MongoDB操作 ...

  6. VMware12多台虚拟机上网设置

    1.根据镜像安装好linux系统, 2.  把网络适配器移除后,重新添加试试 3. 当按照上述配置后还是无法上网,把VMnet0 桥接模式的自动 ,这里重新点击下自动设置(其实没有做到自动配置) 4. ...

  7. linux环境中iostat命令的安装,解决-bash: iostat: command not found问题

    需求说明: 今天在测试环境的主机上,准备通过iostat来查看系统的io情况,发现没有该命令 [root@testvm Packages]# iostat -bash: iostat: command ...

  8. golang-defer坑的本质

    https://blog.csdn.net/hittata/article/details/77836435

  9. pycharm pro版本激活

    这几天行激活pycharm pro版会遇到问题,license server的网址都不可用了.包括各个网站的百度云的破解资源链接都被和谐了,发现最近jetbrains对盗版管得严. 首次安装在注册界面 ...

  10. 随笔 -- IO -- Socket/ServerSocket -- 系统概述

    随笔 -- IO -- Socket/ServerSocket -- Echo(BIO)实例 Java 网络编程 网络编程是指编写运行在多个设备(计算机)的程序,这些设备都通过网络连接起来. java ...