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 sfeve at 2010-03-29 16:23:21 on Problem 3752
高手请不吝赐教...
#include <stdio.h>

int main() {
	int i, j, m, n, flag = 1;
	char letter[100][100], temp;

	enum {
		up = 0, down = 1, left = 2, right = 3
	} direction;
	
	scanf("%d %d", &m, &n);

	for(i=0; i<m; i++) {
		for(j=0; j<n; j++) {
			letter[i][j] = 0;
		}
	}

	i = j = 0; 
	temp = 0;
	letter[i][j] = 'A';
	direction = 3;

	while(flag) {
		temp = letter[i][j];

		switch(direction) {
		case 0:
			--i;
			if(i == -1 || letter[i][j] != 0) {
				++i;
				direction = 3;
			} else {
				letter[i][j] = temp +1;
				if(letter[i][j] > 'Z') {
					letter[i][j] = 'A';
				}	
			}
			break;
		case 1:
			++i;
			if(i == m || letter[i][j] != 0) {
				--i;
				direction = 2;
			} else {
				letter[i][j] = temp +1;
				if(letter[i][j] > 'Z') {
					letter[i][j] = 'A';
				}	
			}
			break;
		case 2:
			--j;
			if(j == -1 || letter[i][j] != 0) {
				++j;
				direction = 0;
			} else {
				letter[i][j] = temp +1;
				if(letter[i][j] > 'Z') {
					letter[i][j] = 'A';
				}	
			}
			break;
		case 3:
			++j;
			if(j == n || letter[i][j] != 0) {
				--j;
				direction = 1;
			} else {
				letter[i][j] = temp +1;
				if(letter[i][j] > 'Z') {
					letter[i][j] = 'A';
				}
			}
			break;
		}

		if(letter[i+1][j] != 0 && letter[i][j+1] != 0 && letter[i-1][j] != 0 && letter[i][j-1] != 0) {
			flag = 0;
		}
	}

	for(i=0; i<m; i++) {
		for(j=0; j<n; j++) {
			if(j == n-1) {
				printf("%c\n", letter[i][j]);
			} else {
				printf("%c   ", letter[i][j]);
			}
		}
	}

	return 1;
}

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