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

贴一段AC了的代码,32ms

Posted by shuidukou at 2013-10-15 00:33:36 on Problem 1009
根据这里面的某位同学的提示,终于做出来了,写的有点乱,原理是这个样子滴:
http://hi.baidu.com/zdg102/item/66f34342080006ab60d7b9a8
代码:
#include<iostream>
#include<algorithm>
#include"math.h"
using namespace std;
#define MAX 1024

int matrix[8][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
int (*image)[3] = new int[MAX][3];
int (*result)[2] = new int[MAX*6][2];
int WIDTH,rows,SIZE;


void print(int (*rs)[2],int n){
	for(int i=0;i<n;i++){
		cout<<rs[i][0]<<" "<<rs[i][1]<<endl;
	}
	cout<<"0 0\n";
}
int getcolor(int (*image)[3],int pos){
	int i=rows-1;
	while(i>=0 && image[i][2]>pos) i--;
	return image[i][0];
}

int calnode(int (*image)[3],int *node){
	int i,j,c=0,k=0;
	for(i=0;i<rows;i++){
		c = image[i][2];
		node[k++] = c;
		node[k++] = c-WIDTH;
		node[k++] = c+WIDTH;
		if(image[i][1]>1){
			c += image[i][1]-1;
			node[k++] = c;
			node[k++] = c-WIDTH;
			node[k++] = c+WIDTH;
		}
	}
	sort(node,node+k);

	int ct=-1;
	for(j=0;j<k;j++){
		if(node[j]<0 || node[j]>=SIZE) continue;
		if(ct<0) node[++ct] = node[j];
		if(node[j] != node[ct]) node[++ct] = node[j];
	}
	return ct+1;
}
int cal_one_pixel(int pos){
	int i,x,y,max=0,clr,newpos;
	int color = getcolor(image,pos);
	for(i=0;i<8;i++){
		x = matrix[i][0];
		y = matrix[i][1];
		newpos = pos + x + y*WIDTH;
		if(newpos<0 || newpos>=SIZE || (x<0 && ((pos % WIDTH)==0)) || (x>0 && (((pos+1) % WIDTH)==0)))
			continue;
		clr = getcolor(image,newpos);
		if(max<abs(color-clr)) max = abs(color-clr);
	}
	return max;
}
int caledge(int *node,int n,int (*rs)[2]){
	int i,k=0,gap;
	for(i=0;i<n-1;i++){
		gap = cal_one_pixel(node[i]);
		rs[k][0] = gap;
		rs[k++][1] = 1;

		if((node[i+1]-node[i])<=1) continue;
		gap = cal_one_pixel(node[i]+1);
		rs[k][0] = gap;
		rs[k++][1] = node[i+1]-node[i]-1;

	}

	gap = cal_one_pixel(node[n-1]);
	rs[k][0] = gap;
	rs[k++][1] = 1;

	int ct=0;
	for(i=1;i<k;i++){
		if(rs[ct][0] == rs[i][0]) rs[ct][1] += rs[i][1];
		else{
			rs[++ct][0] = rs[i][0];
			rs[ct][1] = rs[i][1];
		}
	}
	return ct+1;
}
int main(){
	int *node,nodesize,rssize,start;
	//ifstream fcin("ceshi.txt");
	int color,num; 
	//while(fcin>>WIDTH && WIDTH!=0){
	while(cin>>WIDTH && WIDTH!=0){
		cout<<WIDTH<<endl;
		rows = 0;
		start = 0;
		while(1){
			//fcin>>color>>num;
			cin>>color>>num;
			if(color==0 && num==0) break;

			image[rows][0] = color;
			image[rows][1] = num;
			image[rows][2] = start;
			rows++;
			start += num;
			//cout<<":  "<<image[rows-1][0]<<" "<<image[rows-1][1]<<" "<<image[rows-1][2]<<endl;
		}
		SIZE = start;
		nodesize = 6*rows;
		node = new int[nodesize];
		nodesize = calnode(image,node);
		rssize = caledge(node,nodesize,result);
		print(result,rssize);
	}
	cout<<"0\n";
	//fcin.close();
	system("pause");
	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