示例#1
0
int solution(vector<int> &A) {
    int n = A.size();
    if (n == 0 || n == 1) return 1;
    
    vector<int> fib(n + 2);
    fib[0] = 0;
    fib[1] = 1;
    int fib_count = 2;
    for (int i = 2; ; i++) {
        fib[i] = fib[i - 2] + fib[i - 1];
        if (fib[i] == n + 1) return 1;
        fib_count++;
        if (fib[i] >= n + 1) break;
    }
    
    vector<int> min_step(n + 1);
    min_step[n] = 0;
    for (int i = n - 1; i >= 0; i--) {
        if (!A[i]) {
            min_step[i] = -1;
            continue;
        }
        int curr_min_step = -1;
        for (int j = 2; j < fib_count; j++) {
            if (i + fib[j] > n) break;
            if (i + fib[j] == n) {
                curr_min_step = 0;
                break;
            }
            if ((A[i + fib[j]]) && (min_step[i + fib[j]] >= 0)
                    && (curr_min_step < 0 || min_step[i + fib[j]] < curr_min_step)) {
                curr_min_step = min_step[i + fib[j]];
            }
        }
        min_step[i] = curr_min_step >= 0 ? 1 + curr_min_step : -1;
    }
    
    int min_step_start = -1;
    for (int j = 2; j < fib_count; j++) {
        if (-1 + fib[j] > n) break;
        if (-1 + fib[j] == n) {
            min_step_start = 0;
            break;
        }
        if ((A[-1 + fib[j]]) && (min_step[-1 + fib[j]] >= 0)
                && (min_step_start < 0 || min_step[-1 + fib[j]] < min_step_start)) {
            min_step_start = min_step[-1 + fib[j]];
        }
    }
    
    return min_step_start >= 0 ? 1 + min_step_start : -1;
}
示例#2
0
main()
{
	int srcrow,srccol,destrow,destcol;
	char filename[FILENAMELENGTH];
	char sinput[10];

	show_menu();

	scanf("%s",sinput);
	while(stricmp(sinput,"q")!=0)
	{
		if(stricmp(sinput,"p")==0)
		{
			printf("$ input src point :  col(a~h) >");
			scanf("%s",sinput);				
			srccol=sinput[0]-96;

			printf("$ input src point :  row(1~8) >");
			scanf("%d",&srcrow);

			printf("$ input dest point : col(a~h) >");
			scanf("%s",sinput);
			destcol=sinput[0]-96;

			printf("$ input dest point : row(1~8) >");
			scanf("%d",&destrow);

			//求解
			knight_walk(srcrow,srccol,destrow,destcol);
			int nMinStep=min_step();

			printf("\nTo get from %c%d to %c%d takes %d knight moves.\n\n",srccol+96,srcrow,destcol+96,destrow,nMinStep);
		}
		else if(stricmp(sinput,"f")==0)
		{
			printf("$ input test file name >");
			scanf("%s",filename);

			//处理测试文件
			process_test_file(filename);
		}

		//输入命令
		printf("$ input command >");
		scanf("%s",sinput);
	}

	return 0;
}
示例#3
0
//处理测试文件
void process_test_file(char *filename)
{
	//打开文件并返回文件中的测试示例个数
	int nCaseCount=read_test_file(filename);
	int nMinStep[MAXTEST];

	//求解
	for(int i=0;i<nCaseCount;i++)
	{
		knight_walk(testcase[i].srci,testcase[i].srcj,testcase[i].desti,testcase[i].destj);
		nMinStep[i]=min_step();
	}

	//输出
	output_test_file(filename,nMinStep,nCaseCount);
}
示例#4
0
void on_regret_feedback (int fd, struct message *msg)
{
	printf ("on_regret_feedback\n");
	int result;
	char number[32];
	sscanf (msg->buff, "%s %d", number, &result);
	if (result == 1)
	{
		/*  对方同意悔棋,把棋盘恢复到上一步的局面 */
		set_turn ();
		restore_chessbord ();
		min_step ();
	}
	else 
		show_message_dialog (GTK_MESSAGE_ERROR, "对方拒绝您的请求");
}
示例#5
0
void on_regret (int fd, struct message *msg)
{
	printf ("on_regret\n");

	msg->type = REGRET_FEEDBACK;
	if (accept_request ("对方请求悔棋\n是否同意"))
	{
		sprintf (msg->buff, "%s 1", get_room_number ());
		/*  同意悔棋了,所以要把棋盘恢复到上一步的局面 */
		unset_turn ();
		restore_chessbord ();
		set_regret_flag (FALSE);
		min_step ();
	}
	else
	{
		sprintf (msg->buff, "%s 0", get_room_number ());
	}
	send_message (msg);
}
示例#6
0
void tuner_benchmark(void)
{
	uint32_t current = max_step(0);
	uint32_t band_start = 0;
	uint32_t low_bound = 0, high_bound = 0;
	char buf[20];
	enum { FIND_START, REFINE_START, FIND_END, REFINE_END } state;

	fprintf(stderr, "Testing tuner range. This may take a couple of minutes..\n");

	/* Scan for tuneable frequencies coarsely. When we find something,
	 * do a binary search to narrow down the exact edge of the band.
	 *
	 * This can potentially miss bands or band gaps that are smaller than max_step(freq)
	 * but it is a lot faster than exhaustively scanning everything.
	 */

	/* handle bands starting at 0Hz */
	if (rtlsdr_set_center_freq(dev, 0) < 0)
		state = FIND_START;
	else {
		band_start = 0;
		report_band_start(band_start);
		state = FIND_END;
	}

	while (current < 3e9 && !do_exit) {
		switch (state) {
		case FIND_START:
			/* scanning for the start of a new band */
			if (rtlsdr_set_center_freq(dev, current) < 0) {
				/* still looking for a band */
				low_bound = current;
				current += max_step(current);
			} else {
				/* new band, starting somewhere at or before current */
				/* low_bound < start <= current, refine it */
				high_bound = current;
				state = REFINE_START;
			}
			break;

		case REFINE_START:
			/* refining the start of a band */
			/* low_bound < bandstart <= high_bound */
			if (rtlsdr_set_center_freq(dev, current) == 0) {
				/* current is inside the band */
				/* low_bound < bandstart <= current */
				if (current - low_bound <= min_step(current)) {
					/* close enough. Say the band starts at current and go looking for the end of it. */
					band_start = current;
					report_band_start(band_start);
					low_bound = current;
					state = FIND_END;
				} else {
					/* try halfway between low_bound and current */
					high_bound = current;
					current = (current + low_bound) / 2;
				}
			} else {
				/* current is outside the band */
				/* current < bandstart <= high_bound */
				if (high_bound - current <= min_step(current)) {
					/* close enough. Say the band starts at high_bound and go looking for the end of it. */
					current = low_bound = band_start = high_bound;
					report_band_start(band_start);
					state = FIND_END;
				} else {
					/* try halfway betwen current and high_bound */
					low_bound = current;
					current = (current + high_bound) / 2;
				}
			}
			break;

		case FIND_END:
			/* scanning for the end of the current band */
			if (rtlsdr_set_center_freq(dev, current) == 0) {
				/* still looking for the end of the band */
				low_bound = current;
				current += max_step(current);
			} else {
				/* ran off the end of the band somewhere before current */
				/* low_bound <= bandend < current, refine it */
				high_bound = current;
				state = REFINE_END;
			}
			break;

		case REFINE_END:
			/* refining the end of a band */
			/* low_bound <= bandend < high_bound */
			if (rtlsdr_set_center_freq(dev, current) < 0) {
				/* current is outside the band */
				/* low_bound <= bandend < current */
				if (current - low_bound <= min_step(current)) {
					/* close enough. Say the band ends at low_bound and go looking for another band. */
					report_band(band_start, low_bound);
					low_bound = current;
					state = FIND_START;
				} else {
					/* try halfway between low_bound and current */
					high_bound = current;
					current = (current + low_bound) / 2;
				}
			} else {
				/* current is inside the band */
				/* current <= bandend < high_bound */
				if (high_bound - current <= min_step(current)) {
					/* close enough. Say the band ends at current and go looking for another band. */
					report_band(band_start, current);
					current = low_bound = high_bound;
					state = FIND_START;
				} else {
					/* try halfway betwen current and high_bound */
					low_bound = current;
					current = (current + high_bound) / 2;
				}
			}
			break;
		}
	}

	if (state == FIND_END)
		report_band(band_start, current);
	else if (state == REFINE_END)
		report_band(band_start, low_bound);
}