Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为迷途中的小猫咪们贴个代码

Posted by gzl1988 at 2012-03-08 19:22:10 on Problem 1789
#include <iostream>
using namespace std;

//#define DEBUG
//1789
int n, nDist, nIndex;
char code[2002 * 7];
char s[2002];
char maps[2002];

int calcDist(int a, int b) {
	int k, dist;
	char* i;
	char* j;
	if (a == b) 
		return 0;
	else {
		dist = 0;
		i = code + 7 * a;
		j = code + 7 * b;
		for (k = 0; k < 7; k++) {
			if (*i != *j) {
				dist++;
			}
			i++;
			j++;
		}
		return dist;
	}

}

void getNearest() {
	int i, tmpDist;
	nDist = 100;
	nIndex = -1;
	for (i = 1; i < n; i++) {
		if (maps[i] == 0 && s[i] < nDist) {
			nDist = s[i];
			nIndex = i;
		}
	}
	maps[nIndex] = 1;
	for (i = 1; i < n; i++) {
		if (maps[i] == 0) {
			tmpDist = calcDist(nIndex, i);
			if (tmpDist < s[i])
				s[i] = tmpDist;
		}
	}
}

int process() {
	int i, index;
	int l = 0;
	s[0] = 0;
	maps[0] = 1;
	for (i = 1; i < n; i++) {
		s[i] = calcDist(0, i);
		maps[i] = 0;
	}
	for (i = 0; i < n - 1; i++) {
		getNearest();
		l += nDist;
	}
	return l;
}
int main(int argc, char *argv[])
{
	int i, j, ans;
	while (1) {
		cin >> n;
		if (n == 0) {
			break;
		}
		for (i = 0; i < n ; i++) {
			scanf("%s", code + i * 7);
		}
		code[7 * n] = '\0';
		ans = process();
		cout << "The highest possible quality is 1/" << ans << "." << endl;
	}
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator