05-树6. Path in a Heap (25)

<span style="background-color: rgb(250, 250, 250); font-family: 'Droid Sans', Verdana, 'Microsoft YaHei', Tahoma, sans-serif; line-height: 1em;"><span style="font-size:32px;">05-树6. Path in a Heap (25)</span></span>

  
  
时间限制
150 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue

Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given.

Output Specification:

For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line.

Sample Input:
5 3
46 23 26 24 10
5 4 3
Sample Output:
24 23 10
46 23 10
26 10

思路:完全依葫芦画瓢何头的讲解,非常简单,通过循环比较插入结点和其父结点,遇到哨兵的时候推出循环,找到新结点的插入位置插入就好。
代码:
#include<iostream>
using namespace std;

#define MaxData -10001
#define ElementType int 

typedef struct HeapStruct *MaxHeap;
struct HeapStruct{
	ElementType *Elements;
	int size;
	int Capacity;
};

MaxHeap CreateHeap(int MaxSize);
void Insert_Heap(MaxHeap H, int item);
void Output(MaxHeap H,int n);

int main(int argc, char*[])
{
	int n, m;
	cin >> n >> m;
	MaxHeap H = CreateHeap(n);
	for (int i =1 ; i <=n; i++)
	{
		int data;
		cin >> data;
		Insert_Heap(H, data);
	}
	
	for (int i = m; i > 0; i--)
	{
		int num;
		cin >> num;
		Output(H,num);
	}

	system("pause");
	return 0;
}

MaxHeap CreateHeap(int MaxSize)
{
	MaxHeap H =(MaxHeap)malloc(sizeof(struct HeapStruct));
	H->Elements =(int *) malloc((MaxSize + 1)*sizeof(ElementType));
	H->size = 0;
	H->Capacity = MaxSize;
	H->Elements[0] = MaxData;//因为要建最小堆,因此在0地址存最小数作为哨兵
	return H;
}

void Insert_Heap(MaxHeap H, int item)
{
	int i;
	if (H->size == H->Capacity)
	{
		cout << "最大堆以满"<<endl;
		return;
	}
	i = ++H->size;
	for (; H->Elements[i / 2] > item; i /= 2)
	{
		H->Elements[i] = H->Elements[i / 2];
	}
	H->Elements[i] = item;
}

void Output(MaxHeap H,int n)
{
	for (int i = n; i > 0; i /= 2)
	{
		cout << H->Elements[i];
		if (i == 1)
			cout << endl;
		else
			cout << " ";
	}
}

 
import heapq import copy # 定义状态类 class State: def __init__(self, board, moves=0, parent=None, last_move=None): self.board = board self.moves = moves self.parent = parent self.last_move = last_move def __lt__(self, other): return self.moves < other.moves def __eq__(self, other): return self.board == other.board # 定义转移函数 def move(state, direction): new_board = copy.deepcopy(state.board) for i in range(len(new_board)): if 0 in new_board[i]: j = new_board[i].index(0) break if direction == "up": if i == 0: return None else: new_board[i][j], new_board[i-1][j] = new_board[i-1][j], new_board[i][j] elif direction == "down": if i == len(new_board)-1: return None else: new_board[i][j], new_board[i+1][j] = new_board[i+1][j], new_board[i][j] elif direction == "left": if j == 0: return None else: new_board[i][j], new_board[i][j-1] = new_board[i][j-1], new_board[i][j] elif direction == "right": if j == len(new_board)-1: return None else: new_board[i][j], new_board[i][j+1] = new_board[i][j+1], new_board[i][j] return State(new_board, state.moves+1, state, direction) # 定义A*算法 def astar(start, goal): heap = [] closed = set() heapq.heappush(heap, start) while heap: state = heapq.heappop(heap) if state.board == goal: path = [] while state.parent: path.append(state) state = state.parent path.append(state) return path[::-1] closed.add(state) for direction in ["up", "down", "left", "right"]: child = move(state, direction) if child is None: continue if child in closed: continue if child not in heap: heapq.heappush(heap, child) else: for i, (p, c) in enumerate(heap): if c == child and p.moves > child.moves: heap[i] = (child, child) heapq.heapify(heap) # 测试 start_board = [[1, 2, 3], [4, 5, 6], [7, 8, 0]] goal_board = [[2, 3, 6], [1, 5, 8], [4, 7, 0]] start_state = State(start_board) goal_state = State(goal_board) path = astar(start_state, goal_board) for state in path: print(state.board) 这段代码运行后报错,因为State是不可hash的,那么如何修改才能使得功能一样且能够运行
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值