JD 1204:农夫、羊、菜和狼的故事

2024-09-07 03:18
文章标签 故事 农夫 jd 1204

本文主要是介绍JD 1204:农夫、羊、菜和狼的故事,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

OJ题目:click here~~


#define vegetable_go   0
#define vegetable_come 1
#define sheep_go 2
#define sheep_come 3
#define wolf_go 4
#define wolf_come 5
#define nothing_go 6
#define nothing_come 7using namespace std ;
typedef long long LL ;
string ope[8] = {"vegetable_go" , "vegetable_come" ,"sheep_go" , "sheep_come" ,"wolf_go" , "wolf_come" , "nothing_go" ,"nothing_come" } ;void Print(vector<int> &g){for(int i = 0;i < g.size();i++)cout << ope[g[i]] << endl ;puts("succeed") ;puts("") ;
}bool IsValid(int state , int func){switch(func){case vegetable_go : return (state&1) == 0 && (state&8) == 0 ;case vegetable_come : return (state&1) == 1 && (state&8) == 8 ;case sheep_go : return (state&2) == 0 && (state&8) == 0 ;case sheep_come : return (state&2) == 2 && (state&8) == 8 ;case wolf_go : return (state&4) == 0 && (state&8) == 0 ;case wolf_come : return (state&4) == 4 && (state&8) == 8 ;case nothing_go : return (state&8) == 0 ;case nothing_come : return (state&8) == 8 ;}
}bool IsSafe(int state){int a1, a2 , a4 , a8 ;if(state&1) a1 = 1 ;else a1 = 0 ;if(state&2) a2 = 1 ;else a2 = 0 ;if(state&4) a4 = 1 ;else a4 = 0 ;if(state&8) a8 = 1 ;else a8 = 0 ;if(a1 == a2 && a8 != a1) return false ;if(a2 == a4 && a8 != a2) return false ; ;return true ;
}int gao(int last , int func){switch(func){case vegetable_go : return last|9 ;case vegetable_come : return last&6 ;case sheep_go : return last|10 ;case sheep_come : return last&5 ;case wolf_go : return last|12 ;case wolf_come : return last&3 ;case nothing_go : return last|8 ;case nothing_come : return last&7 ;}
}
bool NotYet(int state , vector<int> &g){return find(g.begin() , g.end(), state) == g.end() ;
}void process(vector<int> &g,vector<int> &op){int last = g.back() ;if(last == 15){Print(op) ;return ;}for(int i = 0;i < 8;i++){if(IsValid(last,i)) {int now = gao(last,i) ;if(!IsSafe(now)) continue ;if(!NotYet(now , g)) continue ;g.push_back(now) ;op.push_back(i) ;process(g , op) ;g.pop_back() ;op.pop_back() ;}}
}int main(){vector<int> g ;vector<int> op ;g.push_back(0) ; //初始状态0000(2),表示农夫,狼,羊,蔬菜都在河岸左边//目标状态1111(2),表示农夫,狼,羊,蔬菜都在河岸右边process(g , op) ;return 0 ;
}


这篇关于JD 1204:农夫、羊、菜和狼的故事的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1143904

相关文章

接下来的这个故事就来自于我的先生,一个交警的口述

这可是没有过的事情。先生是个交通警察,在事故科工作已经五、六年了,对于生离死别、阴阳两隔,用他自己的话说是已经有些麻木了;不用说他,就连我,对那些卷宗里血淋淋的照片都已经有些漠然。他的办公室常有悲悲切切的人来哭诉,他却总能在复议时做到不掺杂感情。我是个爱哭的女人,偏偏先生对于眼泪早已有了职业的免疫力,他说要是每个事故他都要为每个逝者陪眼泪的话,他早就活不下去了,但是今天不同,他分明是掉过泪了。

JD 1474:矩阵幂

OJ题目:click here~~ 题目分析:经典题目,矩阵快速幂。 typedef vector<int> vec ;typedef vector<vec> mat ;int n ;mat mul(mat &A , mat &B){mat C(n , vec(n)) ;for(int i = 0;i < n;i++)for(int j = 0;j < n;j++)for(int k

JD 1497:面积最大的全1子矩阵

OJ题目:click here~~ 题目分析:经典题目。。 const int maxn = 1008 ;int n , m ;int x[maxn][maxn] ;int h[maxn] , Left[maxn] , Right[maxn] ;void check(int &a , int b){if(b > a) a = b ;}void all_1_matrix()

JD 1147:Jugs(一种用最少步骤求解的方法)

OJ题目:click here~~ 题目分析:九度上这道没有要求最少步数,只要得到最后结果即可AC , bfs , dfs都行。最少步骤的方法肯定也能AC啦,分析如下。 输入的三个数:a,b,n;> 由题不定方程ax+by=n必定有解> 如果b=n,则fill B即可,否则用试探法求出这样的两组解(a1,b1)及(a2,b2),其中a1 >0,b1<0;a1是满足方程的最小正整数;a2

JD 1027:欧拉回路

OJ题目:click here~~ 题目分析: 若图G中存在这样一条路径,使得它恰通过G中每条边一次,则称该路径为欧拉路径。若该路径是一个圈,则称为欧拉(Euler)回路。 具有欧拉路径的图称为欧拉图(简称E图)。 无向图存在欧拉回路的充要条件: 一个无向图存在欧拉回路,当且仅当该图拥有奇数度数的顶点的个数为0且该图是连通图。 有向图存在欧拉回路的充要条件: 一

JD 1008:最短路径问题

OJ题目:click here~~ 题目分析:无向图,每条边有长度和花费,求点s到t的最短路径长度和花费。若有相同的最短路径长度,找出最少的花费的那条。 邻接表 + Dijstra + 优先队列 AC_CODE const int maxn = 1008 ;const int inf = 1<<30 ;vector<int> g[maxn] ;int len[maxn][maxn

JD 1520:树的子结构

OJ题目:click here~~ 剑指offer 面试题18 AC_CODE const int maxn = 1008 ;const int maxm = 1008 ;int m , n ;struct Node{int x ;int l ;int r ;Node():l(-1),r(-1){}}A[maxn] , B[maxm];vector<int> g ;void F

JD 1521:二叉树的镜像

OJ题目:click here~~ 题目分析:给一棵二叉树,输出镜像的前序遍历序列 二叉树的镜像的前序遍历序列 = 对原二叉树进行根 右 左 的遍历。 剑指offer 面试题19 AC_CODE const int maxn = 1008 ;LL x[maxn] ;int n ;struct Node{int x ;int l ;int r ;Node():l(-1),r(-1

JD 1368:二叉树中和为某一值的路径

OJ题目:click here~~ 题目分析:找二叉树中和为某一值的路径。。。 剑指offer 面试题25 AC_CODE const int maxn = 10008 ;int n , k ;struct Node{int x ;int l , r ;Node():l(-1),r(-1){}}tree[maxn];void FindPath(int t , int sum ,

JD 1385:重建二叉树

OJ题目:click here~~ 题目分析:给前序遍历序列和中序遍历序列,重构二叉树并输出后序遍历序列 剑指offer 面试题6 AC_CODE int pre[1008] , in[1008] ;struct Node{int x ;Node *left ;Node *right ;};bool buildsubtree(Node*& root , int* spre , in