/ XMU_ACM /

记录详情

Compile Error

Compilation failed: 7 error(s), 3 warnings

/w/foo.cs(2,0): error CS1024: Wrong preprocessor directive
/w/foo.cs(3,0): error CS1024: Wrong preprocessor directive
/w/foo.cs(3,6): error CS1525: Unexpected symbol `namespace', expecting `identifier' or `static'
/w/foo.cs(3,19): error CS1525: Unexpected symbol `;', expecting `identifier' or `static'
/w/foo.cs(4,9): error CS1514: Unexpected symbol `[', expecting `.' or `{'
/w/foo.cs(4,10): warning CS0658: `value' is invalid attribute target. All attributes in this attribute section will be ignored
/w/foo.cs(5,14): warning CS0658: `value' is invalid attribute target. All attributes in this attribute section will be ignored
/w/foo.cs(5,17): warning CS0658: `value' is invalid attribute target. All attributes in this attribute section will be ignored
/w/foo.cs(5,34): error CS1518: Attributes must be attached to class, delegate, enum, interface or struct
/w/foo.cs(5,33): error CS1525: Unexpected symbol `}', expecting `identifier' or `static'
[Hydro](https://hydro.ac)提供评测服务

代码

#include<cstdio>
#include<cmath>
using namespace std;
char arr[10];
int changeId[9][4] = { {-1,-1,3,1},{-1,0,4,2},{-1,1,5,-1},
					{0,-1,6,4},{1,3,7,5},{2,4,8,-1},
					{3,-1,-1,7},{4,6,-1,8},{5,7,-1,-1} };
const int M = 2E+6, N = 1000003;//362897;
int hashTable[M];//hashtable中key为hash值,value为被hash的值 
int next[M];//next表示如果在某个位置冲突,则冲突位置存到hashtable[next[i]] 
int que[N][3], des = 123804765;
int hash(int n) {
	return n % N;
}
bool tryInsert(int n) {
	int hashValue = hash(n);
	while (next[hashValue]) {//如果被hash出来的值得next不为0则向下查找 
		if (hashTable[hashValue] == n)//如果发现已经在hashtable中则返回false 
			return false;
		hashValue = next[hashValue];
	}//循环结束hashValue指向最后一个hash值相同的节点 
	if (hashTable[hashValue] == n)//再判断一遍 
		return false;
	int j = N - 1;//在N后面找空余空间,避免占用其他hash值得空间造成冲突 
	while (hashTable[++j]);//向后找一个没用到的空间 
	next[hashValue] = j;
	hashTable[j] = n;
	return true;
}
void swap(char* ch, int a, int b) { char c = ch[a]; ch[a] = ch[b]; ch[b] = c; }
int bfsHash(int start, int zeroPos) {
	char temp[10];
	int head = 0, tail = 1;
	que[head][0] = start, que[head][1] = zeroPos, que[head][2] = 0;
	while (head != tail) {
		sprintf(temp, "%09d", que[head][0]);
		int pos = que[head][1], k;
		for (int i = 0; i < 4; i++) {
			if (changeId[pos][i] != -1) {
				swap(temp, pos, changeId[pos][i]);
				sscanf(temp, "%d", &k);
				if (k == des)return que[head][2] + 1;
				if (tryInsert(k)) {//插入新状态成功,则说明新状态没有被访问过 
					que[tail][0] = k;
					que[tail][1] = changeId[pos][i];
					que[tail][2] = que[head][2] + 1;
					tail++;
				}
				swap(temp, pos, changeId[pos][i]);
			}
		}
		head++;
	}
}
int main() {
	int n, k;
	scanf("%s", arr);
	for (k = 0; k < 9; k++)
		if (arr[k] == '0')break;
	sscanf(arr, "%d", &n);
	printf("%d", bfsHash(n, k));
	return 0;
}

信息

递交者
类型
自测
题目
八数码
语言
C#
递交时间
2021-10-10 13:17:32
评测时间
2021-10-10 13:17:32
评测机
分数
0
总耗时
0ms
峰值内存
0 Bytes