您的位置:首页 > 其它

HDOJ 1224 记忆搜索

2012-08-12 22:19 246 查看
前两天才做了一道记忆搜索题,所以这道题很快就有思路了。

如果一个路径,它经过的城市的interesting point之和最大,但是这条路径的终点不是起点,则这条路径不能取。

#include <iostream>
using namespace std;

const int MAX_CITY = 105;
int map[MAX_CITY + 1][MAX_CITY + 1];/* 最后一个点是起点 */
int ans[MAX_CITY + 1],point[MAX_CITY + 1],nextCity[MAX_CITY + 1];

int DFS(int n,int cityNum)/* n是当前所在城市 */
{
int maxPoint = 0;/* 从n的下一点i出发,所能积累的最大point值 */
int isReturnable = 0;/* 是否能返回出发点 */
for (int i = (n + 1);i <= (cityNum + 1);i ++)
{
if (map
[i] == 1)
{
int temp = 0;
if (ans[i] == 0)/* 第i个城市的值没有计算过,先计算 */
temp = DFS(i,cityNum);
else
temp = ans[i];
if (temp > maxPoint)
{
maxPoint = temp;
nextCity
= i;
}
if (temp != 0 || (i == (cityNum + 1)))
isReturnable = 1;
}
}
if (isReturnable == 1)
ans
= point
+ maxPoint;
return ans
;
}

int main ()
{
int caseNum = 1;
scanf("%d",&caseNum);
for (int caseCount = 1;caseCount <= caseNum;caseCount ++)
{
memset(map,0,sizeof(map));
memset(ans,0,sizeof(ans));
memset(nextCity,0,sizeof(nextCity));
int cityNum,flightNum;
scanf("%d",&cityNum);
for (int i = 1;i <= cityNum;i ++)
scanf("%d",&point[i]);
point[0] = point[cityNum + 1] = 0;
scanf("%d",&flightNum);
for (int i = 1;i <= flightNum;i ++)
{
int a,b;
scanf("%d%d",&a,&b);
map[a][b] = 1;
}
printf("CASE %d#\n",caseCount);
printf("points : %d\n",DFS(1,cityNum));
printf("circuit : ");
int curCity = 1;
while (curCity != 0)
{
printf("%d->",curCity);
curCity = nextCity[curCity];
}
printf("1\n");
if (caseCount < caseNum)
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: