(国赛B题)沙漠掘金——基于深度优先搜索的沙漠掘金游戏策略求解(下,matlab代码)

1:first_question_1.m
clc
clear
close all
global weather
global path
global maxpath
global daypath
global maxdaypath
global d
global max_;
maxdaypath=[];
max_=0;
d=0;
maxpath=[];
path=[];
daypath=[];
weather=[2 2 1 3 1 2 3 1 2 2 3 2 1 2 2 2 3 3 2 2 1 1 2 1 3 2 1 1 2 2,1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
DFS(1,0);

2:DFS.m
function []= DFS(postion,day)
% 此处显示有关此函数的摘要
%%判断是否终止
global path daypath d;
global weather;
global max_;
global maxpath;
global maxdaypath;
p=path;
da=daypath;
path=[path postion];
daypath=[daypath day];
if(day>30)
path=p;
daypath=da;
return
end

if(postion~=4&&day==30)
path=p;
daypath=da;
return
end

if(postion==4)
vaule=check(path ,daypath)
if(vaule>max_)
max_=vaule;
maxpath=path;
maxdaypath=daypath;
end

d=d+1
path
daypath
path=p;
daypath=da;
return

end
if(weather(day+1)3)
if(postion
1)
[day_cost_thisway]=use_thisway(day,postion,1);
DFS(1,day+day_cost_thisway)
end
if(postion==2)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
end

  if(postion==3)
 [day_cost_thisway]=use_thisway(day,postion,3);
  DFS(3,day+day_cost_thisway)
  end

else
if(postion1)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,4);
DFS(4,day+day_cost_thisway)
end
if(postion
2)
[day_cost_thisway]=use_thisway(day,postion,3);
DFS(3,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,4);
DFS(4,day+day_cost_thisway)
end
if(postion==3)
[day_cost_thisway]=use_thisway(day,postion,3);
DFS(3,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
end
end
path=p;
daypath=da;
return
End

4.use_thisway.m
function [day_use_thisway] = use_thisway(start_day,start_postion,end_postion)
%use_thisway 此处显示有关此函数的摘要
% start_day 表示做决策的那一天,故而start_day+1为进行决策的日期
% day_use_thisway表示到达end_postion那一天减去start_day
%cost
global weather
if(end_postion==start_postion)
day_use_thisway=1;
return
end

short_dis=zeros(4);
short_dis(1,2)=6;
short_dis(1,4)=3;
short_dis(2,3)=2;
short_dis(2,4)=3;
short_dis(3,2)=2;
i=short_dis(start_postion,end_postion);
day_now=start_day;
while(i>0)

if(weather(round(day_now+1))==1)
    day_now=day_now+1;
    i=i-1;
    

elseif(weather(round(day_now+1))==2)
    day_now=day_now+1;
    i=i-1;
   

elseif(weather(round(day_now+1))==3)
    day_now=day_now+1;
    
end 

end

day_use_thisway=day_now-start_day;

end

5:use1.m
function [b,c,d]=use1(day,k,path)%
%time:来的时间
%qi:起点
%zhong:终点
weather=[1,1,0,2,0,1,2,0,1,1,2,1,0,1,1,1,2,2,1,1,0,0,1,0,2,1,0,0,1,1];
cost=[5,7;8,6;10,10];
b=0;%水消耗
c=0;%食物消耗
d=0;%赚钱
if(path(k)==2&&path(k+1)==3)
for i=day(k)+1:day(k+1)
if weather(i)==2
b=b+cost(weather(i)+1,1);
c=c+cost(weather(i)+1,2);
else
b=b+2cost(weather(i)+1,1);
c=c+2
cost(weather(i)+1,2);
end
end
else
if path(k+1)~=3
for i=day(k)+1:day(k+1)
if weather(i)==2
b=b+cost(weather(i)+1,1);
c=c+cost(weather(i)+1,2);
else
b=b+2cost(weather(i)+1,1);
c=c+2
cost(weather(i)+1,2);
end
end
else
for i=day(k)+1:day(k+1)
b=b+3cost(weather(i)+1,1);
c=c+3
cost(weather(i)+1,2);
d=d+1000;
end
end
end

6:check.m
function [money] = check2(path,day)
%UNTITLED4 此处显示有关此函数的摘要
% 此处显示详细说明
%path:当前路径
%money:最大收益,如果当前路径过程中游戏玩家死亡,money为0
%day:天数
%1;起点;2:村庄;3:矿山;4:终点
A=size(path);
m=A(2);
xiaohao_water=zeros(1,m);
xiaohao_food=zeros(1,m);
income=zeros(1,m);
P=0;
for i=1:m-1
[a1,b1,c1]=use1(day,i,path);
xiaohao_water(i)=xiaohao_water(i)+a1;
xiaohao_food(i)=xiaohao_food(i)+b1;
income(i)=income(i)+c1;
end%计算消耗
keypoint=[m];%记录起点,终点和村庄的path位置
for i=m-11
if path(i)==1||path(i)==2
keypoint=[i,keypoint];
end
end

size_keypoint=size(keypoint);
duan_water=zeros(1,size_keypoint(2)-1);
duan_food=zeros(1,size_keypoint(2)-1);
duan_income=zeros(1,size_keypoint(2)-1);
for i=1:size_keypoint(2)-1%获得每一段可补给点之间的消耗
for j=keypoint(i):keypoint(i+1)-1
duan_water(i)=duan_water(i)+xiaohao_water(j);
duan_food(i)=duan_food(i)+xiaohao_food(j);
duan_income(i)=duan_income(i)+income(j);
end
end
total_water=0;
total_food=0;
total_income=0;
for i=1:m
total_water=total_water+xiaohao_water(i);
total_food=total_food+xiaohao_food(i);
total_income=total_income+income(i);
end%计算总消耗
if (total_water3+total_food2<=1200&&total_water5+total_food10<=10000)
money=10000-(total_water5+total_food10)+total_income;
return
end
money_now=10000;
water_now=0;
food_now=0;
for i=1:size_keypoint
if(path(i)==1)
if(total_food>=((1200-3duan_water(i))/2))
money_now=money_now-5
duan_water(i)-10*((1200-3duan_water(i))/2);
food_now=((1200-3
duan_water(i))/2);
water_now=duan_water(i);
else
money_now=money_now-10total_food-5((1200-2total_food)/2);
food_now=total_food;
water_now=((1200-2
total_food)/2);
end
if(money_now<0||3water_now+2food_now>1200)
money=0;
return
else
water_now=water_now-duan_water(i);
food_now=food_now-duan_food(i);
money_now=money_now+duan_income(i);
end
elseif(path(i)==2)
if(duan_water(i)>water_now)
money_now=money-10*(duan_water(i)-water_now);
water_now=duan_water;
end
if(duan_food(i)>food_now)
money_now=money-20*(duan_food(i)-food_now);
food_now=duan_food;
end
if(money_now<0||3water_now+2food_now>1200)
money=0;
return
else
water_now=water_now-duan_water(i);
food_now=food_now-duan_food(i);
money_now=money_now+duan_income(i);
end
elseif(path(i)==4)
money=money_now;
return
end
end

7:first_question_2.m
clc
clear
close all
global weather
global path
global daypath
global d
global max_
global maxpath;
global maxdaypath
max_=0;
d=0;
path=[];
maxdaypath=[];
daypath=[];
weather=[2 2 1 3 1 2 3 1 2 2 3 2 1 2 2 2 3 3 2 2 1 1 2 1 3 2 1 1 2 2,1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1];
DFS(1,0);

8.use2.m
function [b,c,d]=ues2(day,k,path)%
%time:来的时间
%qi:起点
%zhong:终点
weather=[1,1,0,2,0,1,2,0,1,1,2,1,0,1,1,1,2,2,1,1,0,0,1,0,2,1,0,0,1,1];
cost=[5,7;8,6;10,10];
b=0;%水消耗
c=0;%食物消耗
d=0;%赚钱
if path(k+1)=2&&path(k+1)=4
for i=day(k)+1:day(k+1)
b=b+cost(weather(i)+1,1);
c=c+cost(weather(i)+1,2);
end
else
for i=day(k)+1:day(k+1)
b=b+cost(weather(i)+1,1);
c=c+cost(weather(i)+1,2);
d=d+1000;
end
end

9:DFS.m
function []=DFS(postion ,day)
%%判断是否终止
global path daypath d;
global weather;
global max_
global maxpath
global maxdaypath
p=path;
da=daypath;
path=[path postion];
daypath=[daypath day];
if(day>30)
path=p;
daypath=da;
return
end

if(postion~=6&&day==30)

path=p;
daypath=da;
return 

end

if(postion6)
vaule=check(path ,daypath)
if(vaule>max_)
max_=vaule;
maxpath=path;
maxdaypath=daypath;
end
d=d+1
path
daypath
path=p;
daypath=da;
return
end
if(weather(day+1)3)
if(postion
1)
[day_cost_thisway]=use_thisway(day,postion,1);
DFS(1,day+day_cost_thisway)
end
if(postion
2)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
end
if(postion3)
[day_cost_thisway]=use_thisway(day,postion,3);
DFS(3,day+day_cost_thisway)
end
if(postion
4)
[day_cost_thisway]=use_thisway(day,postion,4);
DFS(4,day+day_cost_thisway)
end
if(postion==5)
[day_cost_thisway]=use_thisway(day,postion,5);
DFS(5,day+day_cost_thisway)
end
else

if(postion==1)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,3);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,3,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,4);
DFS(4,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,5);
DFS(5,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,6);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,6,day+day_cost_thisway)

elseif(postion2)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,3);
DFS(3,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,4);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,4,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,5);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,5,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,6);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,6,day+day_cost_thisway)
elseif(postion
3)
[day_cost_thisway]=use_thisway(day,postion,2);
DFS(2,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,3);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income+1000,3,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,4);
DFS(4,day+day_cost_thisway)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,5);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,5,day+day_cost_thisway)
[day_cost_thisway]=use_thisway(day,postion,6);
DFS(6,day+day_cost_thisway)
elseif(postion==4)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,2);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,2,day+day_cost_thisway)

 [day_cost_thisway]=use_thisway(day,postion,3);
  DFS(3,day+day_cost_thisway)
  
 [day_cost_thisway]=use_thisway(day,postion,4);
  DFS(4,day+day_cost_thisway)
  
 [day_cost_thisway]=use_thisway(day,postion,5);
  DFS(5,day+day_cost_thisway)
  
 [day_cost_thisway]=use_thisway(day,postion,6);
  DFS(6,day+day_cost_thisway)

elseif(postion==5)
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,2);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,2,day+day_cost_thisway)
%
% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,3);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income,3,day+day_cost_thisway)

 [day_cost_thisway]=use_thisway(day,postion,4);
  DFS(4,day+day_cost_thisway)

% [water_cost_thisway,food_cost_thisway,day_cost_thisway]=use_thisway(day,postion,5);
% DFS(water_cost+water_cost_thisway, food_cost+food_cost_thisway,income+1000,5,day+day_cost_thisway)

 [day_cost_thisway]=use_thisway(day,postion,6);
  DFS(6,day+day_cost_thisway)

end
end
path=p;
daypath=da;
return
end

10:check.m
function [money] = check(path,day)
%UNTITLED4 此处显示有关此函数的摘要
% 此处显示详细说明
%path:当前路径
%money:最大收益,如果当前路径过程中游戏玩家死亡,money为0
%day:天数
%1;起点;2:村庄;3:矿山;4:终点
A=size(path);
m=A(2);
xiaohao_water=zeros(1,m);
xiaohao_food=zeros(1,m);
income=zeros(1,m);
P=0;
for i=1:m-1
[a1,b1,c1]=use1(day,i,path);
xiaohao_water(i)=xiaohao_water(i)+a1;
xiaohao_food(i)=xiaohao_food(i)+b1;
income(i)=income(i)+c1;
end%计算消耗
keypoint=[m];%记录起点,终点和村庄的path位置
for i=m-11
if path(i)==1||path(i)==3||path(i)==5
keypoint=[i,keypoint];
end
end

size_keypoint=size(keypoint);
duan_water=zeros(1,size_keypoint(2)-1);
duan_food=zeros(1,size_keypoint(2)-1);
duan_income=zeros(1,size_keypoint(2)-1);
for i=1:size_keypoint(2)-1%获得每一段可补给点之间的消耗
for j=keypoint(i):keypoint(i+1)-1
duan_water(i)=duan_water(i)+xiaohao_water(j);
duan_food(i)=duan_food(i)+xiaohao_food(j);
duan_income(i)=duan_income(i)+income(j);
end
end
total_water=0;
total_food=0;
total_income=0;
for i=1:m
total_water=total_water+xiaohao_water(i);
total_food=total_food+xiaohao_food(i);
total_income=total_income+income(i);
end%计算总消耗
if (total_water3+total_food2<=1200&&total_water5+total_food10<=10000)
money=10000-(total_water5+total_food10)+total_income;
return
end
money_now=10000;
water_now=0;
food_now=0;
more_food=0;
for i=1:size_keypoint(2)
if(path(keypoint(i))==1)
if(total_food>=((1200-3duan_water(i))/2))
money_now=money_now-5
duan_water(i)-10*((1200-3duan_water(i))/2);
food_now=((1200-3
duan_water(i))/2);
water_now=duan_water(i);
else
money_now=money_now-10total_food-5((1200-2total_food)/3);
food_now=total_food;
water_now=((1200-2
total_food)/3);
end
if(money_now<0||(3water_now+2food_now)>1200)
if((3duan_water(i)+2duan_water(i))<=1200)
more_food= more_food+food_now-duan_food(i);
food_now=duan_food(i);
water_now=duan_water(i);
else
money=0;
return
end
else
water_now=water_now-duan_water(i);
food_now=food_now-duan_food(i);
money_now=money_now+duan_income(i);
end
elseif(path(keypoint(i))==3||path(keypoint(i))==5)
if(duan_water(i)>water_now)
money_now=money_now-10*(duan_water(i)-water_now);
water_now=duan_water(i);
end
if(duan_food(i)>food_now)
money_now=money_now-20*(duan_food(i)-food_now);
food_now=duan_food(i);
end
if(money_now<0||(3water_now+2food_now)>1200)
if((3duan_water(i)+2duan_water(i))<=1200)
more_food= more_food+food_now-duan_food(i);
food_now=duan_food(i);
water_now=duan_water(i);
else
money=0;
return
end
else
water_now=water_now-duan_water(i);
food_now=food_now-duan_food(i);
money_now=money_now+duan_income(i);
end
elseif(path(keypoint(i))==6)
money=money_now+10*more_food;;
return
end
end
end

你可能感兴趣的:(matlab,数学建模,动态规划,算法,matlab)