algorithm freckles - andstudy/forge GitHub Wiki
-
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 */
-
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())); } }
- ํ๋ฉด 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 );
}
}
- ์ค์์จ ํ์ด์ ๊ฐ์
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
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
<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
-
ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์ฒ์ ์ ํด๋ณด๋๋ฐ. ๋ฌธ์ ํ์ด๋ณด๋ค๋ ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ์ ์ดํด๋ฅผ ์์ฃผ๋ก ์์ฑํ์์ต๋๋ค ^^
#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
- ๋์ธ๋ ํ๋ฆผ ์๊ณ ๋ฆฌ์ฆ! ^^;
- ๊ทธ๋ํ์์ ์ฌ์ฉํ๋ ์๊ณ ๋ฆฌ์ฆ๋ค์ ๋ง์ด ๋ฏธ์งํฉ๋๋ค
#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;
}