algorithm freckles - andstudy/forge GitHub Wiki

ParkPD

  • Presentation Error ๊ฐ€ ๋‚˜๋Š”๊ตฐ์š”. ๋ฌด์‹œํ•˜๋ ค๊ณ  ๋…ธ๋ ฅ์ค‘์ž…๋‹ˆ๋‹ค. :(

    • Presentation Error ํ•ด๊ฒฐ

      // main ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„ ์ˆ˜์ •
      output << setprecision(2) << fixed << s.Solve();
      output << "\n";
      if (i < testCount-1)		// presentation error ๊ฐ€ ๋‚˜์„œ ๋„ฃ์–ด์ค€๋‹ค.
      {
           output << "\n";
      }
      
  • ๊ฐ์ž ์„œ๋กœ์—๊ฒŒ edge ๋ฅผ ๊ฐ€์ง€๋Š” ๊ทธ๋ž˜ํ”„์—์„œ Minimum Spanning Tree ๋ฅผ ๊ตฌํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๊ณ , Prim Algorithm ์„ ์ƒ๊ฐํ•˜๋ฉฐ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

      /* @JUDGE_ID:parkpd 110401 Cpp "test" */
      
      /* @BEGIN_OF_SOURCE_CODE */
      
      #include <iostream>
      #include <vector>
      #include <set>
      #include <string>
      #include <algorithm>
      #include <map>
      #include <limits>
      #include <assert.h>
      #include <iomanip>
      #include <math.h>
      
      //#define _UNIT_TEST
      
      #ifdef _UNIT_TEST
      #include <Windows.h>
      #endif
      
      using namespace std;
      
      // Minimum Spanning Tree -> Prim Algorithm ์„ ์ƒ๊ฐํ•˜๋ฉฐ ํ’€์—ˆ์Œ.
      
      namespace ATUtil
      {
      	bool IsInRange(int value, int from, int to)
      	{
      		return (from <= value) && (value <= to);
      	}
      
      	int ConvertToIndex(char c)
      	{
      		if ('a' <= c && c <= 'z')
      		{
      			return c - 'a';
      		}
      		else
      		{
      			return -1;
      		}
      	}
      
      	char ConvertToChar(int i)
      	{
      		return (char)i + 'a';
      	}
      
      #ifdef _UNIT_TEST
      	class CStopWatch
      	{
      	public:
      		CStopWatch()
      		{
      			m_StartTick = GetTickCount();		// gcc ์—์„œ ์•ˆ ๋  ์ˆ˜ ์žˆ์œผ๋‹ˆ๊นŒ
      		}
      
      		~CStopWatch()
      		{
      			cout << "Time passed : " << GetTickCount() - m_StartTick << " milisec.\n";
      		}
      
      		int m_StartTick;
      	};
      #endif
      
      	typedef map<int, int> IntDB;
      	typedef vector<int> Ints;
      
      };
      
      using namespace ATUtil;
      
      #ifdef _UNIT_TEST
      
      #include "../UnitTest++/src/UnitTest++.h"
      
      #ifdef max
      #undef max
      #endif
      
      int main()
      {
      	UnitTest::RunAllTests();
      
      	char temp;
      	cin >> temp;
      
      	return 0;
      }
      
      #endif
      
      // code implement
      
      namespace ATCode
      {
      	//////////////////////////////////////////////////////////////////////////
      	// CPos
      	class CFreckle
      	{
      	public:
      		CFreckle() : m_X(0.0), m_Y(0.0), m_Dist(numeric_limits<double>::max()), m_Checked(false) {}
      		void Set(double x, double y)
      		{
      			m_X = x;
      			m_Y = y;
      		}
      		double GetDistFrom(const CFreckle& s)
      		{
      			return sqrt(((m_X - s.m_X) * (m_X - s.m_X)) + ((m_Y - s.m_Y) * (m_Y - s.m_Y)));
      		}
      
      		double m_X;
      		double m_Y;
      		double m_Dist;
      		bool m_Checked;
      	};
      
      	//////////////////////////////////////////////////////////////////////////
      	// CFreckleSolver
      	class CFreckleSolver
      	{
      	public:
      		CFreckleSolver(int n) : m_Num(n), m_Index(0), m_Checked(0) {}
      		void Insert(double x, double y)
      		{
      			m_Freckles[m_Index++].Set(x, y);
      		}
      
      		double Solve()
      		{
      			assert(m_Num == m_Index);
      
      			double ret = 0.0;
      
      			// ์‹œ์ž‘์  ์ง€์ •
      			int s = 0;
      			m_Freckles[s].m_Dist = 0.0;		// ์‹œ์ž‘์  ๊ฑฐ๋ฆฌ๋Š” 0.0
      
      			m_Checked = 0;
      			while (m_Checked + 1 < m_Num)
      			{
      				CFreckle& newAdded = m_Freckles[s];
      				newAdded.m_Checked = true;
      				m_Checked++;
      
      				// ๋‚˜๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋…ธ๋“œ์™€ ๊ฑฐ๋ฆฌ ๊ณ„์‚ฐ 
      				// -> ํ˜„์žฌ Freckle ์— ์ €์žฅ๋œ ๊ฐ’๋ณด๋‹ค ๊ฑฐ๋ฆฌ๊ฐ€ ์งง๋‹ค๋ฉด ๊ฐฑ์‹ ํ•œ๋‹ค.
      
      				// newAdded ๋…ธ๋“œ์™€์˜ ์ธ์ ‘ ๋…ธ๋“œ๋ฅผ ๋Œ๋ฉด์„œ ๊ฑฐ๋ฆฌ๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.
      				double dist = 0.0;
      				for (int i = 0; i < m_Num; ++i)
      				{
      					CFreckle& iNode = m_Freckles[i];
      					if (iNode.m_Checked)
      					{
      						continue;
      					}
      
      					dist = newAdded.GetDistFrom(iNode);
      					if (dist < iNode.m_Dist)
      					{
      						iNode.m_Dist = dist;
      					}
      				}
      
      				// ๊ฒ€์‚ฌํ•˜์ง€ ์•Š์€ ๋…ธ๋“œ ์ค‘ ๊ฐ€์žฅ ๊ฑฐ๋ฆฌ๊ฐ€ ์งง๊ฒŒ ์ง€์ •๋œ ๋…ธ๋“œ๋ฅผ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ ํƒ
      				double minDist = numeric_limits<double>::max();
      				for (int i = 0; i < m_Num; ++i)
      				{
      					CFreckle& iNode = m_Freckles[i];
      					if ((!iNode.m_Checked) && (iNode.m_Dist < minDist))
      					{
      						minDist = iNode.m_Dist;
      						s = i;		// ๋‹ค์Œ ๊ฒ€์‚ฌํ•  node ๋ฅผ ์ง€์ •
      					}
      				}
      
      				ret += minDist;
      			}
      
      			return ret;
      		}
      
      		int m_Num;
      		int m_Index;
      		int m_Checked;
      		CFreckle m_Freckles[100];
      	};
      
      	///////////////////////////////////////////////////////////////
      	// CConsole
      	class CConsole
      	{
      	public:
      		static void ConsoleTest(istream& input, ostream& output);
      	};
      
      	void CConsole::ConsoleTest(istream& input, ostream& output)
      	{
      		int testCount = 0;
      		input >> testCount;
      
      		for (int i = 0; i < testCount; ++i)
      		{
      			int posNum = 0;
      			input >> posNum;
      
      			CFreckleSolver s(posNum);
      
      			double x, y;
      			for (int j = 0; j < posNum; ++j)
      			{
      				input >> x >> y;
      				s.Insert(x, y);
      			}
      
      			if (1 <= i)		// presentation error ๊ฐ€ ๋‚˜์„œ ๋„ฃ์–ด์ค€๋‹ค.
      			{
      				output << "\n\n";
      			}
      			output << setprecision(2) << fixed << s.Solve();
      		}
      	};
      
      }
      
      using namespace ATCode;
      
      #ifndef _UNIT_TEST
      
      int main()
      {
      	CConsole::ConsoleTest(cin, cout);
      
      	return 0;
      }
      
      #else
      
      // tests
      
      struct FixtureBase
      {
      	FixtureBase()
      	{
      	}
      
      	stringstream input;
      	stringstream output;
      };
      
      TEST_FIXTURE(FixtureBase, Solve)
      {
      	CFreckleSolver s(3);
      	s.Insert(1.0, 1.0);
      	s.Insert(2.0, 2.0);
      	s.Insert(2.0, 4.0);
      	CHECK_CLOSE(3.41, s.Solve(), 0.01);
      }
      
      TEST_FIXTURE(FixtureBase, ConsoleTest)
      {
      	input << 
      		"1\n"
      		"\n"
      		"3\n"
      		"1.0 1.0\n"
      		"2.0 2.0\n"
      		"2.0 4.0\n"
      		;
      	CConsole::ConsoleTest(input, output);
      	CHECK_EQUAL("3.41", output.str());
      }
      
      TEST_FIXTURE(FixtureBase, ConsoleTest1)
      {
      	input << 
      		"2\n"
      		"\n"
      		"3\n"
      		"1.0 1.0\n"
      		"2.0 2.0\n"
      		"2.0 4.0\n"
      		"\n"
      		"3\n"
      		"1.0 1.0\n"
      		"2.0 2.0\n"
      		"2.0 4.0\n"
      		;
      	CConsole::ConsoleTest(input, output);
      	CHECK_EQUAL("3.41\n\n3.41", output.str());
      }
      
      #endif
      
      /* @END_OF_SOURCE_CODE */
    

kukuman

  • Presentation error ๋‚˜๋„ค์š” OTL

  • ๋ชจ๋“  ์  ์ ๋“ค์ด ์—ฐ๊ฒฐ ๋ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ํ”„๋ง ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์ตœ์†Œ์‹ ์žฅํŠธ๋ฆฌ๋ฅผ ๊ตฌํ•ด์„œ ๊ฐ ๋…ธ๋“œ์˜ ๊ธธ์ด๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

      /*
       * Main.java
       *  java program model for www.programming-challenges.com
       */
      
      import java.io.IOException;
      import java.text.DecimalFormat;
      
      class Main implements Runnable{
          static String ReadLn(int maxLength){  // utility function to read from stdin,
                                                // Provided by Programming-challenges, edit for style only
              byte line[] = new byte [maxLength];
              int length = 0;
              int input = -1;
              try{
                  while (length < maxLength){//Read untill maxlength
                      input = System.in.read();
                      if ((input < 0) || (input == '\n')) break; //or untill end of line ninput
                      line [length++] += input;
                  }
      
                  if ((input < 0) && (length == 0)) return null;  // eof
                  return new String(line, 0, length);
              }catch (IOException e){
                  return null;
              }
          }
      
          public static void main(String args[])  // entry point from OS
          {
              Main myWork = new Main();  // Construct the bootloader
              myWork.run();            // execute
          }
      
          public void run() {
          	int testCaseCount = Integer.parseInt( ReadLn(255).trim() );
          	for(int i=0;i<testCaseCount;i++){
          		ReadLn(255);
          		int pointCount = Integer.parseInt( ReadLn(255).trim() );
          		BackBoard board = new BackBoard(pointCount);
          		for(int j=0;j<pointCount;j++){
          			String line = ReadLn(255);
          			String [] split = line.split(" ");
          			double x = Double.parseDouble(split[0]);
          			double y = Double.parseDouble(split[1]);
          			board.add(new Point(x,y));
          		}
          		double result = board.getMinLengh();
          		DecimalFormat df =new DecimalFormat("###.##");
      
          		System.out.println(df.format( result));
          		System.out.println("");
          	}
          }
      }
      
      
      
      class BackBoard {
      	private int pointCount;
      	private Point [] points;
      	public BackBoard(int pointCount) {
      		points = new Point[pointCount];
      		pointCount = 0;
      	}
      	public void add(Point point) {
      		points[pointCount++] = point;
      	}
      	
      	public int getPointCount(){
      		return this.pointCount;
      	}
      	public double getMinLengh() {
      		boolean [] isVisited = new boolean[pointCount]; //๋ฐฉ๋ฌธ ํ–ˆ๋Š”์ง€ ์—ฌ๋ถ€
      		double [] distance = new double[pointCount]; //๊ฐ ์ •์ ์˜  ๋ฐฉ๋ฌธํ•œ ๊ทธ๋ž˜ํ”„์™€ ์ตœ์†Œ ๊ฑฐ๋ฆฌ
      		
      		for(int i=0;i<pointCount;i++){
      			isVisited[i] = false;
      			distance[i] = Double.MAX_VALUE;
      		}
      		
      		//์‹œ์ž‘์€ ์ฒซ๋ฒˆ์งธ ์ ๋ถ€ํ„ฐ
      		int curPointCursor = 0;
      		distance[curPointCursor] = 0; //์‹œ์ž‘์ ์˜ ๊ฑฐ๋ฆฌ๋Š” ์ž๊ธฐ์ž์‹ ์ด๋ฏ€๋กœ 0
      		
      		while(isVisited[curPointCursor]== false){
      			isVisited[curPointCursor] = true;
      			//๋ฐฉ๋ฌธํ•œ ์ ๊ณผ์˜ ์ตœ์†Œ๊ฑฐ๋ฆฌ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•œ ๋กœ์ง
      			for(int i=0;i<pointCount;i++){
      				double curDistance = points[curPointCursor].getDistance(points[i]);
      				//ํ˜„์žฌ ์„ ํƒ๋œ ์ ๊ณผ์˜ ๊ฑฐ๋ฆฌ๊ฐ€  ๊ธฐ์กด์— ๊ณ„์‚ฐ๋œ ๊ฐ’์ด ์ž‘๋‹ค๋ฉด update
      				if(isVisited[i] == false && distance[i] > curDistance){
      					distance[i] = curDistance;
      				}
      			}
      			
      			double minDistance = Double.MAX_VALUE;
      			curPointCursor = 1;
      			//๋‹ค์Œ ์ˆœํšŒํ•  ์ ์„ ์„ ํƒ ํ•˜๋Š” ๋กœ์ง
      			for(int i=0;i<pointCount;i++){
      				//๋‹ค์Œ ์ˆœํšŒ์ ์€ ๋ฐฉ๋ฌธํ•œ ์ ๊ณผ์˜ ๊ฑฐ๋ฆฌ๊ฐ€ ๊ฐ€์žฅ ๊ฐ€๊นŒ์šด ํ•ญ๋ชฉ์ด ์„ ํƒ ๋œ๋‹ค.
      				if(isVisited[i] == false && distance[i] < minDistance){
      					minDistance = distance[i];
      					curPointCursor = i;
      				}
      			}
      		}
      		
      		double  distSum = 0;
      		//๊ฐ ์„ ์˜ ๊ธธ์ด๋Š” distanc ๋ฐฐ์—ด์˜ ํ•ฉ๊ณผ ๊ฐ™๋‹ค.
      		for(int i=0;i<pointCount;i++){
      			distSum += distance[i];
      		}
      		return distSum;
      	}
      
      }
      
      class Point {
      	private double x;
      	private double y;
      
      	public Point(double x, double y) {
      		this.x = x;
      		this.y = y;
      	}
      
      	public double getDistance(Point point2) {
      		return  Math.sqrt( Math.pow(x-point2.x, 2) + Math.pow(y-point2.y, 2)) ;
      	}
      
      }
    
  • test Case

      public class FrecklesTestCase extends TestCase {
      	public void testAddPoint(){
      		BackBoard board = new BackBoard(3);
      		board.add(new Point(1.0,1.0));
      		board.add(new Point(2.0,2.0));
      		board.add(new Point(2.0,4.0));
      		assertEquals(3,board.getPointCount());
      	}
      	
      	public void testPointDistance(){
      		Point point1 = new Point(1.0,1.0);
      		Point point2 = new Point(2.0,2.0);
      		Point point3 = new Point(2.0,4.0);
      		assertEquals(Math.sqrt(2.0), point1.getDistance(point2));
      		assertEquals(2.0, point2.getDistance(point3	));
      	}
      	
      	public void testMinLengh(){
      		BackBoard board = new BackBoard(3);
      		board.add(new Point(1.0,1.0));
      		board.add(new Point(2.0,2.0));
      		board.add(new Point(2.0,4.0));
      		assertEquals("3.41", String.format("%.2f",  board.getMinLengh()));
      	}
      }
    

Outbreak

๋ฌธ์ œ ์š”์•ฝ

  • ํ‰๋ฉด xy ์— ์—ฌ๋Ÿฌ์ ์ด ์žˆ์„ ๋•Œ, ์ด ์ ๋“ค์„ ๋ชจ๋‘ ์ง์„ ์œผ๋กœ ์ž‡๋Š” ์ตœ์†Œ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋ผ

๋ฌธ์ œ ํ•ด๊ฒฐ

  • ์—ด๋ฆฐ๋ชฉ๋ก, ๋‹ซํžŒ๋ชฉ๋ก์„ ๋งŒ๋“ฌ
  • ์ฒ˜์Œ ์‹œ์ž‘ ์‹œ ๋‹ซํžŒ๋ชฉ๋ก์— 1๊ฐœ๋ฅผ ๋„ฃ์–ด๋‘๊ณ  ๋‚˜๋จธ์ง€๋Š” ์—ด๋ฆฐ ๋ชฉ๋ก
    • ๋‹ซํžŒ๋ชฉ๋ก์˜ ์ ๋“ค๊ณผ ์ตœ์†Œ ๊ฑฐ๋ฆฌ์˜ ์ ์„ ์—ด๋ฆฐ๋ชฉ๋ก์—์„œ ์„ ํƒ
    • ์„ ํƒ ๋œ ์ ์„ ๋‹ซํžŒ๋ชฉ๋ก์— ์ถ”๊ฐ€. ์—ด๋ฆฐ๋ชฉ๋ก์—์„  ์ œ๊ฑฐ.
    • ์œ„ ๋‘ ๊ณผ์ • ๋ฐ˜๋ณต
  • ํฌ๋ฃจ์Šค์นผ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ˜•ํƒœ์ธ ๋“ฏ?

์†Œ๊ฐ

  • ์˜ˆ์ „์— ํ’€์–ด๋ดค๋˜ ๋ฌธ์ œ.. ์ด๋ฒˆ์ฃผ๋Š” ์‰ฝ๊ฒŒ ํ•œ๋ฌธ์ œ.. ใ…‹ใ…‹
  • ์˜ˆ์ „ ์ฝ”๋“œ๋ฅผ ๋ณด๋‹ˆ, ํ’€์ด ๊ตฌ์กฐ๋ฅผ ์ •ํ˜•ํ™” ์‹œํ‚ค๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์ง€๋งŒ..
  • scanf/printf ๋ฅผ ์™œ ์“ฐ๋ƒ๋Š” ์ด์•ผ๊ธฐ๋ฅผ ๋“ค์—ˆ๋˜ ๊ธฐ์–ต์ด ๋‚˜๋„ค์š” ์–ดํœด~

ํ’€์ด

    #include <cstdio>
    #include <cmath>
    #include <ctime>
    #include <vector>
    #include <algorithm>
    #include <string>
    
    using namespace std;
    
    class InputStuff;
    class OutputStuff;
    
    typedef vector<InputStuff> INPUT;
    typedef vector<InputStuff>::iterator INPUTItr;
    
    typedef vector<OutputStuff> OUTPUT;
    typedef vector<OutputStuff>::iterator OUTPUTItr;
    
    void InputProc(INPUT& Inputs );
    void MainProc( const INPUT& Inputs, OUTPUT& Outputs );
    void OutputProc( const OUTPUT& Outputs );
    
    //-------------------------------------------------------------
    
    int main(int argc, char* argv[])
    {
    	int maxtestcase;
    	scanf( "%d", &maxtestcase );
    
    	while( maxtestcase > 0 )
    	{		
    		INPUT inputs;
    		OUTPUT outputs;
    
    		// ์ž…๋ ฅ๊ณ„
    		InputProc( inputs );
    
    		// ์ฒ˜๋ฆฌ๊ณ„
    		MainProc( inputs, outputs );
    
    		// ์ถœ๋ ฅ๊ณ„
    		OutputProc( outputs );
    	
    		if( --maxtestcase > 0)
    	            printf("\n");
    	}
    
    	return 0;
    }
    
    //---------------------------------------------------------------
    class PosStuff
    {
    public:
    
    	PosStuff( float Dx = 0.f, float Dy = 0.f ) : X(Dx), Y(Dy){}
    	
    	PosStuff& operator()( float Dx, float Dy )
    	{
    		X = Dx;
    		Y = Dy;
    		return *this; 
    	}
    
    	// ๋‘ ๋…ธ๋“œ๊ฐ„์˜ ๊ฑฐ๋ฆฌ๊ณ„์‚ฐ
    	float operator-(const PosStuff& Dst)
    	{		
    		float xx = abs(this->X-Dst.X);
    		float yy = abs(this->Y-Dst.Y);
    	
    		return sqrt(xx*xx + yy*yy);
    	}
    public:
    	float X;
    	float Y;
    };
    
    class InputStuff
    {
    public:
    	vector<PosStuff> Nodes;
    };
    
    class OutputStuff
    {
    public:
    	float Length;
    };
    
    
    void InputProc(INPUT& Inputs )
    {
    	int maxample;
    	scanf( "%d", &maxsample);
    
    	InputStuff stuff;
    
    	while( maxsample > 0 )
    	{
    		int input = 0;
    
    		PosStuff xy;
    
    		scanf("%f %f", &xy.X, &xy.Y );
    
    		stuff.Nodes.push_back( xy );
    	
    		maxsample--;
    	}
    
    	Inputs.push_back( stuff );
    }
    
    float FindSmallestCost( const vector<PosStuff>& Nodes )
    {
    	vector<PosStuff> Opend;
    	vector<PosStuff> Closed;
    
    	// ๋‹ซํžŒ ๋ชฉ๋ก๊ณผ ์—ด๋ฆฐ ๋ชฉ๋ก์„ ๋งŒ๋“ค์–ด์ค€๋‹ค.
    	Closed.push_back( Nodes.front() );
    	Opend.assign( Nodes.begin()+1, Nodes.end() );
    	
    	// ์ „์ฒด ๋น„์šฉ
    	float total = 0.f;
    
    	while( true )
    	{
    		// ์ตœ์†Œ ๋น„์šฉ
    		float min = 9999999.f;
    
    		// ์ตœ์†Œ ๋น„์šฉ์ด ๋˜๋Š” ์—ด๋ฆฐ ๋ชฉ๋ก์˜ ํƒ€๊ฒŸ ๋…ธ๋“œ
    		int minnode = 0;
    
    		for( int i=0; i<Closed.size(); i++ )
    		{
    			for( int j=0; j<Opend.size(); j++ )
    			{
    				// ์—ด๋ฆฐ ๋ชฉ๋ก ์ค‘ ์ตœ์†Œ ๋น„์šฉ์ด ๋“œ๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
    				float dist = Closed[i]-Opend[j];
    				if( min > dist )
    				{
    					min = dist;
    					minnode = j;
    				}
    			}
    		}
    
    		// ๋‹ซํžŒ ๋ชฉ๋ก์— ์‚ฝ์ž…
    		Closed.push_back( Opend[minnode] );
    
    		// ์—ด๋ฆฐ ๋ชฉ๋ก์—์„œ๋Š” ์‚ญ์ œ
    		Opend.erase( Opend.begin() + minnode );
    
    		//printf("์ตœ์†Œ๋น„์šฉ ๋…ธ๋“œ๋Š” %d ๋ฒˆ์งธ์ด๊ณ  ๋น„์šฉ์€ %f \n", minnode, min);
    		total += min;
    
    		if( Opend.empty() )
    		{
    			//printf("๋ชจ๋‘ ๋‹ซํžŒ ๋…ธ๋“œ๊ฐ€ ๋์Šต๋‹ˆ๋‹ค! ์†Œ๋น„ ๋น„์šฉ์€ %0.2f ์ž…๋‹ˆ๋‹ค!\n", total);
    			break;
    		}
    	}
    	
    	return total;
    }
    
    void MainProc( const INPUT& Inputs, OUTPUT& Outputs )
    {	
    	for( int i=0; i<Inputs.size(); i++ )
    	{
    		vector<PosStuff> nodes;
    
    		nodes.assign( Inputs[i].Nodes.begin(), Inputs[i].Nodes.end() );
    
    		OutputStuff stuff;
    		stuff.Length = FindSmallestCost( nodes );
    
    		Outputs.push_back( stuff );
    	}
    }
    
    void OutputProc( const OUTPUT& Outputs )
    {
    	for( int i=0; i<Outputs.size(); i++ )
    	{		
    		printf( "%0.2f\n", Outputs[i].Length );
    	}	
    }

CynicJJ

  • ์ค€์„์”จ ํ’€์ด์™€ ๊ฐ™์Œ

Point.vb

    Public Class Point
    
    	Sub New(ByVal x As Double, ByVal y As Double)
    		_x = x
    		_y = y
    	End Sub
    
    	Private _x As Double
    	Private _y As Double
    
    	ReadOnly Property x() As Double
    		Get
    			Return _x
    		End Get
    	End Property
    
    	ReadOnly Property y() As Double
    		Get
    			Return _y
    		End Get
    	End Property
    
    End Class

Freckles.vb

    Public Class Freckles
    
    	Shared Function GetDistance(ByVal p1 As Point, ByVal p2 As Point) As Double
    		Dim xDiff As Double = p1.x - p2.x
    		Dim yDiff As Double = p1.y - p2.y
    
    		Return Math.Sqrt(Math.Pow(xDiff, 2) + Math.Pow(yDiff, 2))
    	End Function
    
    	Shared Function FormatNumber(ByVal number As Double, ByVal digitsAfterDot As Integer) As Double
    		Dim strNumber As String = Microsoft.VisualBasic.FormatNumber(number, digitsAfterDot)
    		Return Double.Parse(strNumber)
    	End Function
    
    	Shared Function GetDistanceSum(ByVal points As List(Of Point)) As Double
    		Dim copied(points.Count - 1) As Point
    		points.CopyTo(copied)
    		Dim freckles As List(Of Point) = New List(Of Point)(copied)
    
    		Dim distanceSum As Double = 0
    
    		Dim checked As List(Of Point) = New List(Of Point)
    		checked.Add(freckles(0))
    		freckles.RemoveAt(0)
    
    		While freckles.Count > 0
    			Dim minDistance As Double = Double.MaxValue
    			Dim indexOfMin As Integer
    			For Each aChecked As Point In checked
    				For i As Integer = 0 To freckles.Count - 1
    					Dim distance As Double = GetDistance(aChecked, freckles(i))
    					If distance < minDistance Then
    						minDistance = distance
    						indexOfMin = i
    					End If
    				Next
    			Next
    
    			checked.Add(freckles(indexOfMin))
    			freckles.RemoveAt(indexOfMin)
    
    			distanceSum += minDistance
    		End While
    
    		Return FormatNumber(distanceSum, 2)
    	End Function
    
    End Class

!FrecklesTest.vb

    <TestClass()> _
    Public Class FrecklesTest
    
    	Private _points As List(Of Point) = New List(Of Point)
    
    	Sub New()
    		_points.Add(New Point(1.0, 1.0))
    		_points.Add(New Point(2.0, 2.0))
    		_points.Add(New Point(2.0, 4.0))
    	End Sub
    
    	<TestMethod()> _
    	Public Sub FormatNumberTest()
    		Assert.AreEqual(1.41, Freckles.FormatNumber(1.40834, 2))
    	End Sub
    
    	<TestMethod()> _
    	Public Sub GetDistanceTest()
    		Dim p1 As Point = New Point(1.0, 1.0)
    		Dim p2 As Point = New Point(2.0, 2.0)
    
    		Dim distance As Double = Freckles.GetDistance(p1, p2)
    		Assert.AreEqual(1.41, Freckles.FormatNumber(distance, 2))
    	End Sub
    
    	<TestMethod()> _
    	Public Sub GetDistanceSumTest()
    		Assert.AreEqual(3.41, Freckles.GetDistanceSum(_points))
    	End Sub
    
    	<TestMethod()> _
    	Public Sub GetDistanceSumTestMore()
    		Dim points As List(Of Point) = New List(Of Point)
    		points.Add(New Point(1.0, 1.0))
    		points.Add(New Point(2.0, 1.0))
    		points.Add(New Point(1.0, 2.0))
    		points.Add(New Point(1.0, 3.0))
    
    		Assert.AreEqual(3.0, Freckles.GetDistanceSum(points))
    	End Sub
    
    	<TestMethod()> _
    	Public Sub GetDistanceSumTestMoreMore()
    		Dim points As List(Of Point) = New List(Of Point)
    		points.Add(New Point(1.0, 1.0))
    		points.Add(New Point(2.0, 1.0))
    		points.Add(New Point(1.0, 5.0))
    		points.Add(New Point(2.0, 5.0))
    
    		Assert.AreEqual(6.0, Freckles.GetDistanceSum(points))
    	End Sub
    
    End Class

soomong

  • ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ฒ˜์Œ ์ ‘ํ•ด๋ณด๋Š”๋ฐ”. ๋ฌธ์ œํ’€์ด๋ณด๋‹ค๋Š” ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜์˜ ์ดํ•ด๋ฅผ ์œ„์ฃผ๋กœ ์ž‘์„ฑํ•˜์˜€์Šต๋‹ˆ๋‹ค ^^

      #include "stdafx.h"
      #include "math.h"
      
      #define _UNIT_TEST
      #define MAXV 100
      
      #define MAXINT 142		// sqrt(pow(0,2)+pow(100,2)) = 141.42
      
      typedef struct
      {
      	double x;
      	double y;
      }point;
      
      typedef struct
      {
      	point points[MAXV+1];
      	int numPoint;	/* ์ •์  ๊ฐœ์ˆ˜ */
      }graph;
      
      class CFreckles
      {
      private:
      	graph g;
      
      public:
      	CFreckles();
      	void addPoint(double x, double y);
      	double prim();
      	double getDistance(point a, point b);
      
      };
      
      CFreckles::CFreckles()
      {
      	g.numPoint = 0;
      }
      
      void CFreckles::addPoint(double x, double y)
      {
      	g.points[g.numPoint].x = x;
      	g.points[g.numPoint].y = y;
      	g.numPoint++;
      }
      
      
      double CFreckles::getDistance(point a, point b)
      {
      	return sqrt(pow(a.x - b.x,2) + pow(a.y - b.y,2));
      }
      
      
      double CFreckles::prim()
      {
      	bool intree[MAXV];		/* ์ •์ ์ด ํŠธ๋ฆฌ์— ๋“ค์–ด์žˆ๋Š”์ง€ ์—ฌ๋ถ€ */
      	double distance[MAXV];	/* ์‹œ์ž‘์ ์œผ๋กœ๋ถ€ํ„ฐ์˜ ๊ฑฐ๋ฆฌ */
      	int curPoint;		/* ์ง€๊ธˆ ์ฒ˜๋ฆฌํ•  ์ •์  */
      	double tempDist;			
      	double resultDist = 0;
      	int i,j;
      
      	// ๊ฐ์ข… ์ •๋ณด๋ฅผ ์ €์žฅํ•  ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
      	for( i=0; i< g.numPoint; i++)
      	{
      		intree[i] = false;
      		distance[i] = MAXINT;
      	}
      
      	distance[0] = MAXINT;
      	curPoint = 0;
      
      	// ์‹œ์ž‘ point 0 ๊ณผ ๋‚˜๋จธ์ง€ ๋ชจ๋“  point ๋“ค์˜ ๊ฑฐ๋ฆฌ๋ฅผ ์ดˆ๊ธฐ๊ฐ’์œผ๋กœ.
      	for( i=1; i< g.numPoint; i++)
      	{
      		// point 0 ๊ณผ point i ์˜ ๊ฑฐ๋ฆฌ
      		distance[i] = getDistance(g.points[0],g.points[i]);
      	}
      
      	intree[0] = true;
      	curPoint = 1;
      
      	while(intree[curPoint] == false)
      	{
      
      		// point i ์— ๋Œ€ํ•˜์—ฌ
      		for( i=0; i< g.numPoint; i++)
      		{
      			// i ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์„ ์ด ์žˆ๋Š”๊ฒฝ์šฐ continue
      			if( intree[i] == true )
      				continue;		
      			
      			// i ๋กœ๋ถ€ํ„ฐ j ๊นŒ์ง€์˜ ๋ชจ๋“  ์„ ์˜ distance ์ฒดํฌ
      			for( j=0; j< g.numPoint; j++)
      			{
      				// j ๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์„ ์ด ์žˆ๋Š”๊ฒฝ์šฐ continue
      				if( intree[j] == true )
      					continue;		
      
      				tempDist = getDistance(g.points[i],g.points[j]);
      
      				// ์ตœ์†Œ distance ์ด๊ณ  ๊ฐ™์€ point ๋ผ๋ฆฌ ์—ฐ๊ฒฐํ•˜๋Š”๊ฒŒ ์•„๋‹๊ฒฝ์šฐ ๊ฐฑ์‹ 
      				if( distance[j] > tempDist && i != j )
      				{
      					distance[j] = tempDist;
      				}
      			}
      
      			intree[i] = true;
      		}
      
      		curPoint++;
      
      	}
      
      	// 1๋ถ€ํ„ฐ๊ฐ€ ์œ ํšจํ•œ distance
      	for(int i=1; i<g.numPoint; i++)
      	{
      		resultDist += distance[i];
      	}
      
      	return resultDist;
      }
      
      
      #if defined(_UNIT_TEST)
      #include "UnitTest++.h"
      
      
      struct ConstructorTest
      {
      
      	ConstructorTest() : cf()
      	{
      
      	}
      	CFreckles cf,cf2,cf3;
      	graph g,g1,g2,g3;
      	
      };
      
      TEST_FIXTURE( ConstructorTest, FrecklesTest )
      {
      		
      	/*1.0 1.0
      	  2.0 2.0
      	  2.0 4.0 */
      	cf.addPoint(1.0,1.0);
      	cf.addPoint(2.0,2.0);
      	cf.addPoint(2.0,4.0);
      	CHECK_EQUAL(3.41,cf.prim());
      
      	/* 1.0 1.0
      	   2.0 2.0
      	   2.0 4.0
      	   1.0 0.0 */
      	cf2.addPoint(1.0,1.0);
      	cf2.addPoint(2.0,2.0);
      	cf2.addPoint(2.0,4.0);
      	cf2.addPoint(1.0,0.0);
      	CHECK_EQUAL(4.41,cf2.prim());
      
      	/* 3.0 1.0
      	   1.0 2.0
      	   3.0 3.0
      	   2.0 4.0
      	   4.0 5.0 */
      	cf3.addPoint(3.0,1.0);
      	cf3.addPoint(1.0,2.0);
      	cf3.addPoint(3.0,3.0);
      	cf3.addPoint(2.0,4.0);
      	cf3.addPoint(4.0,5.0);
      	CHECK_EQUAL(7.87,cf3.prim());
      
      }
      
      TEST_FIXTURE( ConstructorTest,  getDistanceTest )
      {
      	g.points[0].x = 1.0;
      	g.points[0].y = 1.0;	
      
      	g.points[1].x = 2.0;
      	g.points[1].y = 2.0;
      
      	g.points[2].x = 2.0;
      	g.points[2].y = 4.0;	
      
      	g.points[3].x = 4.0;
      	g.points[3].y = 3.0;
      
      	CHECK_EQUAL(1.41,cf.getDistance(g.points[0],g.points[1]));
      	CHECK_EQUAL(2,cf.getDistance(g.points[1],g.points[2]));
      	CHECK_EQUAL(2.23,cf.getDistance(g.points[2],g.points[3]));
      
      	g.points[4].x = 0;
      	g.points[4].y = 0;
      	g.points[5].x = 100;
      	g.points[5].y = 100;
      
      	CHECK_EQUAL(141.42,cf.getDistance(g.points[4],g.points[5]));
      
      }
      
      int main(int argc, _TCHAR* argv[])
      {
         return UnitTest::RunAllTests();
      }
      
      #else
      
      int main(int argc, _TCHAR* argv[])
      {
      	return 0;
      }
      
      #endif
    

Mastojun

๋ฌธ์ œ ํ’€์ด

  • ๋Œ€์„ธ๋Š” ํ”„๋ฆผ ์•Œ๊ณ ๋ฆฌ์ฆ˜! ^^;
  • ๊ทธ๋ž˜ํ”„์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜๋“ค์€ ๋งŽ์ด ๋ฏธ์ง„ํ•ฉ๋‹ˆ๋‹ค

์†Œ์Šค

    #include <stdio.h>
    #include <math.h>
    
    struct point
    {
    	float x, y;
    };
    
    int	n;			///< ์ฃผ๊ทผ๊นจ ๊ฐœ์ˆ˜
    point	points[101];		///< ๊ฐ ์ฃผ๊ทผ๊นจ
    float	Distance[101][101];	///< ๊ฐ ์ฃผ๊ทผ๊นจ์™€ ๋‹ค๋ฅธ ์ฃผ๊ทผ๊นจ์˜ ๊ฑฐ๋ฆฌ
    bool	isUse[101];		///< ์‚ฌ์šฉ์ค‘์ธ์ง€ ๊ธฐ์–ต
    
    void InputData()
    {
    	scanf("%d", &n);;
    
    	for(int i = 1; i <= n; i++ )
    	{
    		scanf("%f %f",&points[i].x, &points[i].y);
    	}
    }
    
    void ConstructTable()
    {
    	float tempDist;
    
    	for(int i = 1; i <= n; i++ )
    	{
    		for(int j = 1; j <= n; j++ )
    		{
    			if( i == j )
    			{
    				Distance[i][j] = 0;
    			}
    			else
    			{
    				tempDist = sqrt( pow(points[i].x - points[j].x, 2) + 
    						 pow(points[i].y - points[j].y, 2) );
    
    				Distance[i][j] = tempDist;
    			}
    		}
    
    		isUse[i] = false;
    	}
    }
    
    float ShortFreckles()
    {
    	int FrecklesPoint;
    	bool isInit = false;
    	float MinDist;
    
    	for( int i = 1; i <= n; i++ )
    	{
    		if( isUse[i] == false ) continue;
    
    		for( int j = 1; j <= n; j++ )
    		{
    			if( i == j ) continue;
    			if( isUse[j] ) continue;
    
    			if( isInit == false )
    			{
    				MinDist = Distance[i][j];
    				FrecklesPoint = j;
    				isInit = true;
    			}
    			else
    			{
    				if( MinDist > Distance[i][j])
    				{
    					MinDist = Distance[i][j];
    					FrecklesPoint = j;
    				}
    			}
    		}			
    	}
    
    	isUse[FrecklesPoint] = true;
    
    	return MinDist;
    }
    
    float GetResult()
    {
    	float result = 0;
    
    	isUse[1] = true;
    
    	for(int i = 2; i <= n; i++ )
    	{
    		result += ShortFreckles();
    	}
    
    	return result;
    }
    
    int main()
    {
    	int testcase;
    
    	scanf("%d", &testcase);
    
    	while( testcase --> 0 )
    	{
    		InputData();
    		ConstructTable();
    
    		printf("%.2f\n", GetResult());
    
    		if(testcase)putchar('\n');
    	}	
    
    
    	return 0;
    }
โš ๏ธ **GitHub.com Fallback** โš ๏ธ