首页 > ACM题库 > HDU-杭电 > HDU 1802 Black and white painting-栈-[解题报告] C++
2013
12-23

HDU 1802 Black and white painting-栈-[解题报告] C++

Black and white painting

问题描述 :

You are visiting the Centre Pompidou which contains a lot of modern paintings. In particular you notice one painting which consists solely of black and white squares, arranged in rows and columns like in a chess board (no two adjacent squares have the same colour). By the way, the artist did not use the tool of problem A to create the painting.

Since you are bored, you wonder how many 8 × 8 chess boards are embedded within this painting. The bottom right corner of a chess board must always be white.

输入:

The input contains several test cases. Each test case consists of one line with three integers n, m and c. (8 ≤ n, m ≤ 40000), where n is the number of rows of the painting, and m is the number of columns of the painting. c is always 0 or 1, where 0 indicates that the bottom right corner of the painting is black, and 1 indicates that this corner is white.

The last test case is followed by a line containing three zeros.

输出:

For each test case, print the number of chess boards embedded within the given painting.

样例输入:

8 8 0
8 8 1
9 9 1
40000 39999 0
0 0 0

样例输出:

0
1
2
799700028

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
using namespace std;
struct node{
	int row,col;
}a[30];
int ans;
int main(){
	int i,j,n;
	cin>>n;
	char c;
	char s[1000];
	node x,y;
	while(n--){
		cin>>c>>i>>j;
		a[c-'A'].row=i;
		a[c-'A'].col=j;
	}
	while(scanf("%s",s)!=EOF){
		stack<node>sk;
		ans=0;
		for(i=0;s[i];i++){
			if(s[i]==')'){
				y=sk.top();sk.pop();
				x=sk.top();sk.pop();
				//cout<<x.row<<" "<<x.col<<"|"<<y.row<<" "<<y.col<<endl;
				if(x.col!=y.row){ans=-1;break;}
				ans+=x.row*x.col*y.col;
				x.col=y.col;
				sk.push(x);
			}else if(s[i]=='('){}
			else{
				sk.push(a[s[i]-'A']);
			}
		}
		if(ans<0)cout<<"error"<<endl;
		else cout<<ans<<endl;
	}
return 0;
}

解题报告转自:http://blog.csdn.net/xinglely/article/details/8856600


,
  1. 第二个方法挺不错。NewHead代表新的头节点,通过递归找到最后一个节点之后,就把这个节点赋给NewHead,然后一直返回返回,中途这个值是没有变化的,一边返回一边把相应的指针方向颠倒,最后结束时返回新的头节点到主函数。