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

第50题留念

Posted by walker01 at 2010-06-14 20:34:31 on Problem 1048
此题参考了标程才通过,贴出代码与大家分享:

Memory: 172K  Time: 0MS 
Language: C++  Result: Accepted 

Source Code 
#include <stdio.h>
#include <string.h>

char g[120][120], m[30], f[300];
int opd[50];

char* genFormula(int row, int col, char* fp)
{
	char op, ch;

	op = g[row][col];
	g[row][col] = '*';

	//inversion
	if(op == 'o')
	{
		fp = genFormula(row, col-1, fp);
		*fp++ = op;
		return fp;
	}
	
	//AND or OR gate
	if(op=='>' || op==')')
	{
		fp = genFormula(row-1, col-1, fp);
		fp = genFormula(row+1, col-1, fp);
		*fp++ = op;
		return fp;
	}

	//input
	if(op>='A' && op<='Z')
	{
		*fp++ = op;
		return fp;
	}

	//try up
	if(g[row-1][col]!=' ' && g[row-1][col]!='*')
	{
		ch = g[row-1][col];
		while(ch == '|')
		{
			g[row---1][col] = '*';
			ch = g[row-1][col];
		}
		fp = genFormula(row-1, col, fp);
	}

	//try down
	if(g[row+1][col]!=' ' && g[row+1][col]!='*')
	{
		ch = g[row+1][col];
		while(ch == '|')
		{
			g[row+++1][col] = '*';
			ch = g[row+1][col];
		}
		fp = genFormula(row+1, col, fp);
	}

	//try left
	if(g[row][col-1]!=' ' && g[row][col-1]!='*')
	{
		ch = g[row][col-1];
		while(ch == '-')
		{
			g[row][col---1] = '*';
			ch = g[row][col-1];
		}
		fp = genFormula(row, col-1, fp);
	}

	//try right
	if(g[row][col+1]!=' ' && g[row][col+1]!='*')
	{
		ch = g[row][col+1];
		while(ch == '-')
		{
			g[row][col+++1] = '*';
			ch = g[row][col+1];
		}
		fp = genFormula(row, col+1, fp);
	}
	return fp;
}

int logicValue()
{
	int top = 0;
	char *fp = f;

	char ch;
	while(ch = *fp++)
	{
		if(ch>='A' && ch<='Z')
			opd[top++] = m[ch-'A'] - '0';
		else if(ch == 'o')
			opd[top-1] = !opd[top-1];
		else if(ch == '>')
		{
			--top;
			opd[top-1] |= opd[top];
		}
		else if(ch == ')')
		{
			--top;
			opd[top-1] &= opd[top];
		}
	}
	return opd[top-1];
}

int main()
{
	char ch;
	int r, c, row, col;

	while(true)
	{
		row = col = -1;
		memset(g, ' ', sizeof(g));
		memset(f, 0, sizeof(f));

		//read circuit
		for(r=1; ;++r)
		{
			ch = getchar();
			if(ch==EOF || ch=='*')
				break;

			c = 0;
			while(ch != '\n')
			{
				g[r][++c] = ch;
				if(row<0 && ch=='?')
				{
					row = r;
					col = c;
				}
				ch = getchar();
			}
		}

		//end of file
		if(ch == EOF)
			break;

		//generate formula
		genFormula(row, col, f);

		//read input
		while(scanf("%s", m) == 1)
		{
			getchar();
			if(m[0] == '*')
				break;

			//logical operation
			printf("%d\n", logicValue());
		}
		putchar('\n');
	}
	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