您的位置:首页 > 其它

九度oj 1091

2015-08-19 15:31 190 查看
题目1091:棋盘游戏

时间限制:1 秒

内存限制:32 兆

特殊判题:否

提交:1438

解决:387

题目描述:

有一个6*6的棋盘,每个棋盘上都有一个数值,现在又一个起始位置和终止位置,请找出一个从起始位置到终止位置代价最小的路径:

1、只能沿上下左右四个方向移动

2、总代价是没走一步的代价之和

3、每步(从a,b到c,d)的代价是c,d上的值与其在a,b上的状态的乘积

4、初始状态为1

每走一步,状态按如下公式变化:(走这步的代价%4)+1。

输入:

第一行有一个正整数n,表示有n组数据。

每组数据一开始为6*6的矩阵,矩阵的值为大于等于1小于等于10的值,然后四个整数表示起始坐标和终止坐标。

输出:

输出最小代价。

样例输入:
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
0 0 5 5


样例输出:
23


来源:2005年上海交通大学计算机研究生机试真题
#include<iostream>
#include<queue>
using namespace std;
#define false 0
#define true 1
int maze[6][6];
struct N
{
int x,y;
int state;
int cost;
};
int mark[6][6][4];
N temp1;
queue<N> q;
int mcost,x1,y2;
int go[][2]={
1,0,
0,1,
-1,0,
0,-1};
void bfs(N n)
{
q.push(n);
int ccost,x,y;
while(q.empty()==false)
{

N now=q.front();
q.pop();

for(int i=0;i<4;i++)
{
x=now.x+go[i][0];
y=now.y+go[i][1];
if(x>=0&&x<6&&y>=0&&y<6)
{

ccost=maze[x][y]*now.state;
if(((now.cost+ccost)<mark[x][y][ccost%4])&&((now.cost+ccost)<mark[x1][y2][ccost%4]))
{
mark[x][y][ccost%4]=now.cost+ccost;
N temp;
temp.x=x;
temp.y=y;
temp.cost=now.cost+ccost;
temp.state=ccost%4+1;

q.push(temp);
}
}
}
}
}
int main()
{
int n;
cin>>n;

while(n--)
{
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++)
{
cin>>maze[i][j];
for(int k=0;k<4;k++)
{
mark[i][j][k]=100000;}
}
}

temp1.cost=0;
temp1.state=1;
mcost=100000;
cin>>temp1.x>>temp1.y>>x1>>y2;
bfs(temp1);
for(int i=0;i<4;i++)
{
if(mcost>mark[x1][y2][i])
{
mcost=mark[x1][y2][i];
}
}
cout<<mcost<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: