1.Catch That Cow
#include<iostream>
#include<queue>
using namespace std;
const int maxn=100001;
struct status{
int time; //所需时间
int position; //当前位置
//bool isFind=false;
};
queue<status> wait;
int findCow(status p,int cowPos,bool find[]){ //查找结点相关的下一个
if(p.position==cowPos)return p.time;
if(p.position>0&&!find[p.position-1]){
find[p.position-1]=true;
if(p.position-1==cowPos)return p.time+1;
else {
status next;
next.position=p.position-1;
next.time=p.time+1;
wait.push(next);
//cout<<next.position<<endl;
}
}
if(p.position<maxn&&!find[p.position+1]){
find[p.position+1]=true;
if(p.position+1==cowPos)
{
//cout<<p.time+1<<endl;
return p.time+1;
}
else {
status next;
next.position=p.position+1;
next.time=p.time+1;
wait.push(next);
//cout<<next.position<<endl;
}
}
if(p.position*2<=maxn&&!find[p.position*2]){
find[p.position*2]=true;
if(p.position*2==cowPos)return p.time+1;
else{
status next;
next.position=p.position*2;
next.time=p.time+1;
wait.push(next);
//cout<<next.position<<endl;
}
}
return -1;
}
int main(){
int n,k;
while(cin>>n>>k){
while (!wait.empty()) wait.pop();
bool find[maxn]={false};
status farmer;
farmer.position=n;
farmer.time=0;
if(k==n)cout<<0<<endl;
else wait.push(farmer);
while(!wait.empty()){
int jud=findCow(wait.front(),k,find);
if(jud>0)
{
cout<<jud<<endl;
break;
}
else{
wait.pop();
}
}
}
return 0;
}
2.Find The Mutiple
#include<iostream>
#include<queue>
using namespace std;
void BFS(int n){
queue<long long> myQueue;
myQueue.push(1);
while(!myQueue.empty()){
long long current=myQueue.front();
myQueue.pop();
if(current%n==0){
cout<<current<<endl;
return;
}
myQueue.push(current*10);
myQueue.push(current*10+1);
}
}
int main(){
int n;
while(cin>>n&&n!=0){
BFS(n);
}
return 0;
}
3.玛雅人的密码
#include<iostream>
#include<map>
#include<string>
#include<queue>
using namespace std;
map<string,int> M;//M[str]表示str经历的交换次数
queue<string> Q;
string Swap(string str, int i){
//将字符串i位与i+1位呼唤
string newStr=str;
char tmp=newStr[i];
newStr[i]=newStr[i+1];
newStr[i+1]=tmp;
return newStr;
}
bool Judge(string str){
if(str.find("2012",0)==string::npos)return false;
else return true;
}
int BFS(string str){
//广度优先搜索
M.clear();//清空map
while(!Q.empty())Q.pop();//清空队列
Q.push(str);
M[str]=0;
while(!Q.empty()){
str=Q.front();
Q.pop();
for(int i=0;i<str.size()-1;i++){
string newStr=Swap(str,i);
if(M.find(newStr)==M.end()){
M[newStr]=M[str]+1;
if(Judge(newStr)==true)return M[newStr];
else Q.push(newStr);
}
else continue;
}
}return -1;
}
int main(){
int n;
string str;
while(cin>>n>>str){
if(Judge(str)==true)cout<<0<<endl;
else{
cout<<BFS(str)<<endl;
}
}
return 0;
}
4. A Knight's Journey
(..没保存好代码电脑关闭了...就没有了,王道书上也有这个答案 :)
5.神奇的口袋
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=25;
bool visit[maxn];
int goods[maxn];
int n; //物品个数
int Count=0;//总共可行的方案数
bool DFS(int sum,int position){
if(sum==40){
Count++;
return true;
}
for(int i=position;i<n;i++){
if(visit[i]||sum+goods[i]>40)
continue;
visit[i]=true;
if(DFS(sum+goods[i],i+1))
return true;
visit[i]=false;
}
return false;
}
bool cmp(int x,int y){
return x>y;
}
int main(){
while(cin>>n){
Count=0;
memset(visit,false,sizeof(visit));
for(int i=0;i<n;i++){
cin>>goods[i];
}
sort(visit,visit+n,cmp);
if(DFS(0,0)) cout<<Count<<endl;
}
}
6.八皇后
#include<bits/stdc++.h>
using namespace std;
int a[95][10];//解
int c[10];
int total = 0;
//深度优先遍历
void dfs(int cur){//起始位置
if(cur == 8){
total++;
for(int i = 0; i < 8; i++){
a[total][i] = c[i];
}
}else{
for(int i = 0; i < 8; i++){
c[cur] = i;//标记位置
int ok = 1;//默认是所求的解
for(int j = 0; j < cur; j++){
if(c[j] == i || cur - i == j - c[j] || cur + i == c[j] + j){
ok = 0;
break;//此方案行不通啊
}
}
if(ok){//i循环中此路可行
dfs(cur + 1);//检测下一行
}
}
}
}
int main(){
int N;
while(scanf("%d", &N) != EOF){
dfs(0);//从索引号0开始逐行检测是否有通路
for(int i = 0; i < 8; i++){//8皇后
printf("%d", a[N][i] + 1);//根据需要取方案
}
//cout<<endl;
printf("\n");
}
return 0;
}