-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
76 changed files
with
42 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width"/><title>404: This page could not be found</title><meta name="next-head-count" content="3"/><link rel="preload" href="/_next/static/css/cea7fc6ccc7bac98.css" as="style" crossorigin=""/><link rel="stylesheet" href="/_next/static/css/cea7fc6ccc7bac98.css" crossorigin="" data-n-g=""/><noscript data-n-css=""></noscript><script defer="" crossorigin="" nomodule="" src="/_next/static/chunks/polyfills-c67a75d1b6f99dc8.js"></script><script src="/_next/static/chunks/webpack-6f336cc197ef678d.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/framework-66d32731bdd20e83.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/main-aa55ffd08992d156.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/pages/_app-e287481ee6e74633.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/pages/_error-ee5b5fb91d29d86f.js" defer="" crossorigin=""></script><script src="/_next/static/owMDksqZMVYMHGpa0q1xs/_buildManifest.js" defer="" crossorigin=""></script><script src="/_next/static/owMDksqZMVYMHGpa0q1xs/_ssgManifest.js" defer="" crossorigin=""></script></head><body><div id="__next"><script>!function(){try{var d=document.documentElement,c=d.classList;c.remove('light','dark');var e=localStorage.getItem('theme');if('system'===e||(!e&&true)){var t='(prefers-color-scheme: dark)',m=window.matchMedia(t);if(m.media!==t||m.matches){d.style.colorScheme = 'dark';c.add('dark')}else{d.style.colorScheme = 'light';c.add('light')}}else if(e){c.add(e|| '')}if(e==='light'||e==='dark')d.style.colorScheme=e}catch(e){}}()</script><div style="font-family:system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";height:100vh;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center"><div style="line-height:48px"><style>body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}</style><h1 class="next-error-h1" style="display:inline-block;margin:0 20px 0 0;padding-right:23px;font-size:24px;font-weight:500;vertical-align:top">404</h1><div style="display:inline-block"><h2 style="font-size:14px;font-weight:400;line-height:28px">This page could not be found<!-- -->.</h2></div></div></div></div><script id="__NEXT_DATA__" type="application/json" crossorigin="">{"props":{"pageProps":{"statusCode":404}},"page":"/_error","query":{},"buildId":"owMDksqZMVYMHGpa0q1xs","nextExport":true,"isFallback":false,"gip":true,"scriptLoader":[]}</script></body></html> | ||
<!DOCTYPE html><html lang="en"><head><meta charSet="utf-8"/><meta name="viewport" content="width=device-width"/><title>404: This page could not be found</title><meta name="next-head-count" content="3"/><link rel="preload" href="/_next/static/css/cea7fc6ccc7bac98.css" as="style" crossorigin=""/><link rel="stylesheet" href="/_next/static/css/cea7fc6ccc7bac98.css" crossorigin="" data-n-g=""/><noscript data-n-css=""></noscript><script defer="" crossorigin="" nomodule="" src="/_next/static/chunks/polyfills-c67a75d1b6f99dc8.js"></script><script src="/_next/static/chunks/webpack-6f336cc197ef678d.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/framework-66d32731bdd20e83.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/main-aa55ffd08992d156.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/pages/_app-e287481ee6e74633.js" defer="" crossorigin=""></script><script src="/_next/static/chunks/pages/_error-ee5b5fb91d29d86f.js" defer="" crossorigin=""></script><script src="/_next/static/ouFxnvAPXOOQRIKArTLFG/_buildManifest.js" defer="" crossorigin=""></script><script src="/_next/static/ouFxnvAPXOOQRIKArTLFG/_ssgManifest.js" defer="" crossorigin=""></script></head><body><div id="__next"><script>!function(){try{var d=document.documentElement,c=d.classList;c.remove('light','dark');var e=localStorage.getItem('theme');if('system'===e||(!e&&true)){var t='(prefers-color-scheme: dark)',m=window.matchMedia(t);if(m.media!==t||m.matches){d.style.colorScheme = 'dark';c.add('dark')}else{d.style.colorScheme = 'light';c.add('light')}}else if(e){c.add(e|| '')}if(e==='light'||e==='dark')d.style.colorScheme=e}catch(e){}}()</script><div style="font-family:system-ui,"Segoe UI",Roboto,Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";height:100vh;text-align:center;display:flex;flex-direction:column;align-items:center;justify-content:center"><div style="line-height:48px"><style>body{color:#000;background:#fff;margin:0}.next-error-h1{border-right:1px solid rgba(0,0,0,.3)}@media (prefers-color-scheme:dark){body{color:#fff;background:#000}.next-error-h1{border-right:1px solid rgba(255,255,255,.3)}}</style><h1 class="next-error-h1" style="display:inline-block;margin:0 20px 0 0;padding-right:23px;font-size:24px;font-weight:500;vertical-align:top">404</h1><div style="display:inline-block"><h2 style="font-size:14px;font-weight:400;line-height:28px">This page could not be found<!-- -->.</h2></div></div></div></div><script id="__NEXT_DATA__" type="application/json" crossorigin="">{"props":{"pageProps":{"statusCode":404}},"page":"/_error","query":{},"buildId":"ouFxnvAPXOOQRIKArTLFG","nextExport":true,"isFallback":false,"gip":true,"scriptLoader":[]}</script></body></html> |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"pageProps":{"tags":["react","JavaScript","deepdive","useState","hook","ํด๋ก์ ","์๋ช ์ฃผ๊ธฐ","Virtual DOM","Algorithm","๋ฐฑ์ค","gold","dfs","ํ๋ก๊ทธ๋๋จธ์ค","lv3","bfs","์ด๋ถํ์"],"currentTag":"hook","posts":[{"slug":"posts/React/React useState ํ ๊ณผ ํด๋ก์ ","title":"React useState ํ ๊ณผ ํด๋ก์ ","date":"2024-10-26T18:00:24.000Z","image":"index.png","summary":"ํด๋ก์ ๋ฅผ ์ด์ฉํ React useState ํ ์ ์๋ ์๋ฆฌ๋ฅผ ์ดํดํด๋ณด์","tags":["react","JavaScript","deepdive","useState","hook","ํด๋ก์ "],"content":"# React useState ํ ๊ณผ ํด๋ก์ \r\n\r\nํด๋ก์ ์ ๋ํด ๊ฐ๋ ์ ์๊ณ ์์์ง๋ง, ์ฌ์ค ๋ฉด์ ์ง๋ฌธ๋๋ ์ฐ์ด๋ ๊ฒ์ด๊ณ , ์ค์ ๋ก ์ด๋ป๊ฒ ์ฐ์ด๋์ง์ ๋ํด์๋ ์๋ฟ์ง ๋ชปํ ๋ถ๋ถ๋ ์์๋ค. ๊ทธ๋ฌ๋ ๋์ค react deep dive๋ฅผ ๊ณต๋ถํ๋ฉฐ, react ๊ฐ๋ฐ์๊ฐ ์๋ง๋ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉํ๋ **useState**์ ํด๋ก์ ๊ฐ ์ฐ์ธ๋ค๋ ์ฌ์ค์ ์์๋ค! \r\n์ด๋ป๊ฒ ์ฐ์ด๋ ์ง ํจ๊ป ์ดํด๋ณด์.\r\n\r\n\r\n\r\n## ์ํ๊ฐ์ ์ด๋ป๊ฒ ๊ด๋ฆฌํ ๊น\r\n๋ณดํต useState์ ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ ๊ฒ์ด๋ค\r\n```js\r\nimport { useState } from 'react'\r\n\r\nconst [state, setState] = useState\r\n```\r\n์ธ์๋ก state์ ์ด๊น๊ฐ์ ๋๊ฒจ์ฃผ๊ณ , ๋ง์ผ ์๋ฌด๊ฒ๋ ์๋๊ฒจ์ฃผ๋ฉด ์ด๊น๊ฐ์ undefined์ผ ๊ฒ์ด๋ค.\r\nํ ์ ๋ฐํ ๊ฐ์ ๋ฐฐ์ด์ด๊ณ , ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ์์๋ state ๊ฐ ์์ฒด์ด๋ฉฐ, ๋ ๋ฒ์งธ ์์๋ setState ํจ์๋ฅผ ํตํด ํด๋น state ๊ฐ์ ๋ณ๊ฒฝํ ์ ์๋ค.\r\n\r\n\r\n**๋ฆฌ์กํธ์์ ๋ ๋๋ง์ ํจ์ ์ปดํฌ๋ํธ์ return์ ์คํํ ๋ค์, ์คํ ๊ฒฐ๊ณผ๋ฅผ ์ด์ ์ ๋ฆฌ์กํธ ํธ๋ฆฌ์ ๋น๊ตํด ๋ฆฌ๋ ๋๋ง์ด ํ์ํ ๋ถ๋ถ๋ง ์ ๋ฐ์ดํธํด ์ด๋ค์ง๋ค.**\r\n๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๋ ๋๋ง ๋ฐฉ์์ด๋ ๋ฉ์ปค๋์ฆ์ด ๋ค๋ฅธ ๋ณ์๋ฅผ ํตํด์ ์ํ๊ฐ์ ๊ด๋ฆฌํ๋ ๊ฒ์ ์ ์ ํ์ง ๋ชปํ๋ค. (์ด์ ๊ธ react virtualDOM ์ฐธ๊ณ )\r\n\r\n๊ทธ๋ ๋ค๋ฉด ๋ค์ ์ฝ๋๋ฅผ ์ดํด๋ณด์.\r\n\r\n```js\r\nimport React from 'react'\r\n\r\nconst Component = () => {\r\n const [,triggerRender] = useState()\r\n let state = 'hello'\r\n\r\n function handleButtonClick() {\r\n state = 'hi'\r\n triggerRender()\r\n }\r\n return (\r\n <>\r\n <h1>{state}</h1>\r\n <button onClick={handleButtonClick}>hi</button>\r\n </>\r\n )\r\n}\r\n```\r\nuseState ๋ฐํ๊ฐ์ ๋ ๋ฒ์งธ ์์๋ฅผ ์คํํด ๋ฆฌ์กํธ์ ๋ ๋๋ง์ด ์ผ์ด๋๊ฒ๋ ๋ณ๊ฒฝํ๋ค. \r\n๊ทธ๋ผ์๋ ์ฌ์ ํ ๋ฒํผ ํด๋ฆญ์ state์ ๋ณ๊ฒฝ๋ ๊ฐ์ด ๋ ๋๋ง๋๊ณ ์์ง ์๋ค. \r\n\r\n๊ทธ ์ด์ ๋ ๋ฆฌ์กํธ์ ๋ ๋๋ง์ **ํจ์ ์ปดํฌ๋ํธ์์ ๋ฐํํ ๊ฒฐ๊ณผ๋ฌผ์ธ return์ ๊ฐ์ ๋น๊ตํด ์คํ๋๊ธฐ ๋๋ฌธ์ด๋ค.** \r\n\r\n์ฆ, ๋งค๋ฒ ๋ ๋๋ง์ด ๋ฐ์ํ ๋๋ง๋ค ํจ์๋ ์๋กญ๊ฒ ์คํ์ด ๋๊ณ , ์คํํ ํจ์์์ state๋ ๋งค๋ฒ hello๋ก ์ด๊ธฐํ ๋๋ฏ๋ก ์๋ฌด๋ฆฌ state๋ฅผ ๋ณ๊ฒฝํด๋ hello๋ก ์ด๊ธฐํ ๋๋ ๊ฒ์ด๋ค. \r\n\r\n๊ทผ๋ฐ ๋ ๋๋ง์ด ๋ ๋๋ง๋ค ์ด๊ธฐํ๋๋ ๋ณ์(๊ฐ)์๋ ๋ฌ๋ฆฌ, useState์ ๊ฒฐ๊ณผ๊ฐ์ ์ด๋ป๊ฒ ๊ทธ ๊ฐ์ ์ ์งํ ๊น?\r\n\r\nuseState์ ๊ฒฐ๊ณผ ๊ฐ์ด ์ ์ง๋๋๋ก, state๋ฅผ ํจ์๋ก ํ์ฌ state ๊ฐ์ ํธ์ถํ ๋๋ง๋ค ํ์ฌ state๋ฅผ ๋ฐํํ๊ฒ ํด๋ณด์.\r\n```js\r\nfunction useState(initialValue) {\r\n\tlet initialState = initialValue;\r\n\t\r\n\tfunction state() {\r\n\t\treturn initialState\r\n\t}\r\n\tfunction setState(newValue) {\r\n\t\tinitialState = newValue\r\n\t}\r\n\treturn [state, setState];\r\n}\r\nconst [value, setState] = useState(0);\r\nsetValue(1);\r\nconsole.log(value()); // 1\r\n```\r\n์์ ์ฝ๋๋ ๋์์ง ์์ง๋ง, ์ฐ๋ฆฌ์๊ฒ ์ต์ํ useStateํ ์ state๋ฅผ ํจ์๊ฐ ์๋ ์์์ฒ๋ผ ์ฌ์ฉํ๊ณ ์๋ค.\r\n\r\n## ํด๋ก์ ๋ฅผ ์ด์ฉํด ์ํ๋ฅผ ๊ด๋ฆฌํ๋ useState\r\n์ด๋ฅผ ์ํด์ ๋ฆฌ์กํธ๋ **ํด๋ก์ **๋ฅผ ์ด์ฉํ ๊ฒ์ด๋ค. \r\nuseState๋ ํด๋ก์ ๋ฅผ ํตํด useState ๋ด๋ถ์ ์ ์ธ๋ ํจ์(setState)๊ฐ ํจ์์ ์คํ์ด ์ข ๋ฃ๋ ์ดํ(useState๊ฐ ํธ์ถ๋ ์ดํ)์๋ ์ง์ญ๋ณ์์ธ state๋ฅผ ๊ณ์ ์ฐธ์กฐํ ์ ์๋ค.\r\n\r\nuseState ์๋ ๋ฐฉ์์ ๋๋ต์ ์ผ๋ก ํ๋ด ๋ธ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.\r\n\r\n```js\r\nconst MyReact = (function() {\r\n\tconst global = {}\r\n let index = 0\r\n \r\n function useState(initialState){\r\n \tif(!global.states) {\r\n \t// ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒด์ states ๋ฐฐ์ด ์ด๊ธฐํ, ์ต์ด ์ ๊ทผ์ด๋ฉด ๋น ๋ฐฐ์ด๋ก\r\n \tglobal.states = []\r\n }\r\n // states ์ ๋ณด๋ฅผ ์กฐํํด์, ํ์ฌ ์ํ๊ฐ์ด ์๋์ง ํ์ธ\r\n // ์๋ค๋ฉด ์ด๊น๊ฐ์ผ๋ก ์ค์ \r\n const currentState = global.states[index] || initialState\r\n // ์์์ ์กฐํํ ๊ฐ์ผ๋ก states์ ๊ฐ ์ ๋ฐ์ดํธ\r\n global.states[index] = currentState\r\n \r\n // ์ฆ์์คํํจ์๋ก setter ๋ง๋ฌ\r\n const setState = (function() {\r\n \t// ํด๋ก์ ๋ก index๋ฅผ ๊ฐ๋ฌ๋์ด์ ๋์ผํ index์ ์ ๊ทผ์ด ๊ฐ๋ฅ\r\n \tlet currentIndex = index\r\n return function(value){\r\n \tglobal.states[currentIndex] = value\r\n //์ปดํฌ๋ํธ ๋ ๋๋ง์ด ๋ค์ด๊ฐ๋ ๋ถ๋ถ์ด๋ค.(์ค์ ์ฝ๋๋ ์๋ต)\r\n }\r\n })()\r\n // useState๋ฅผ ์ธ ๋๋ง๋ค index๋ฅผ ํ๋์ฉ ์ถ๊ฐํ๋๋ฐ, ์ด๋ ํ๋์ state๋ง๋ค\r\n // index๊ฐ ํ ๋น๋์ด์์ด, ๊ทธ index๊ฐ ๋ฐฐ์ด์ ๊ฐ(global.states)๋ฅผ ๊ฐ๋ฆฌํค๊ณ ,\r\n // ํ์ํ ๋๋ง๋ค ๊ทธ ๊ฐ์ ๊ฐ์ ธ์ค๊ฒ ํ๋ ๊ฒ์ด๋ค.\r\n index = index + 1\r\n \r\n return [currentState,setState]\r\n}\r\n\r\nfunction Component() {\r\n\tconst [value, setValue] = useState(0);\r\n}\r\n})();\r\n```\r\n\r\n์ค์ ๋ฆฌ์กํธ ์ฝ๋์์๋ useReducer๋ฅผ ์ด์ฉํด ๊ตฌํ๋์ด ์์ด ์ฝ๊ฐ์ ์ฐจ์ด๊ฐ ์๋ค.\r\n\r\n์๋ฌดํผ ์ฌ๊ธฐ์ ํจ์์ ์คํ์ด ๋๋ฌ์์๋ ํจ์๊ฐ ์ ์ธ๋ ํ๊ฒฝ์ ๊ธฐ์ตํ ์ ์๋ ๋ฐฉ๋ฒ์ด ๋ฐ๋ก ํด๋ก์ ์ธ ๊ฒ์ด๋ค. ๋ง์ฝ ํด๋ก์ ๊ฐ ์๋ค๋ฉด, `setState`๋ ํญ์ `index`์ ํ์ฌ ๊ฐ์ ์์กดํ๊ฒ ๋๋ค. ์ฆ, ์ปดํฌ๋ํธ๊ฐ ์ฌ๋ฌ ์ํ๋ฅผ ๊ฐ๊ณ ์์ ๋ ๋ง์ง๋ง `index`๋ง ์ฐธ์กฐํ๋ฏ๋ก, `setState`๊ฐ ์ฌ๋ฐ๋ฅธ ์์น๋ฅผ ์ฐธ์กฐํ์ง ์๊ฒ ๋๋ ๊ฒ์ด๋ค.\r\n\r\n๋งค๋ฒ ์คํ๋๋ ํจ์ ์ปดํฌ๋ํธ ํ๊ฒฝ์์ state์ ๊ฐ์ ์ ์งํ๊ณ ์ฌ์ฉํ๊ธฐ ์ํด ๋ฆฌ์กํธ๋ ํด๋ก์ ๋ฅผ ํ์ฉํ๊ณ ์๋ค.\r\n\r\n`\r\nํ ์ ๋ํ ๊ตฌํ์ฒด๋ฅผ github์์ ํ๊ณ ์ฌ๋ผ๊ฐ๋ค๋ณด๋ฉด __SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED ๋ผ๋ ๋ฌธ๊ตฌ๋ฅผ ๋ง๋๊ฒ๋๋ค(๋ฌด์ญ๋ค ใ ใ )\r\n์์ ์ฝ๋๋ Preact์ ๊ตฌํ์ ๊ธฐ์ค์ผ๋ก ํ๊ณ ์๋ค. Preact๋ react์ ๊ฒฝ๋ํ ๋ฒ์ ์ผ๋ก,\r\n๋๋ถ๋ถ์ ๋ฆฌ์กํธ API๋ฅผ ์ง์ํ๊ณ ์๋ค. \r\n`\r\n## ๊ฒฐ๋ก \r\nReact์ `useState`๋ ํด๋ก์ ๋ฅผ ํตํด ์ํ๊ฐ์ ์์ ์ ์ผ๋ก ์ ์งํ๋ฉฐ, ํจ์ ์ปดํฌ๋ํธ๊ฐ ์ฌ๋ฌ ๋ฒ ํธ์ถ๋๋๋ผ๋ **๊ฐ ์ํ๊ฐ์ด ๊ณ ์ ํ ์์น์ ์ ์ฅ**๋ ์ ์๊ฒ ํ๋ค. `useState`๊ฐ ๋ฐํํ๋ `setState` ํจ์๋ ์์ฑ ๋น์์ ์ํ ์์น(`index`)๋ฅผ ํด๋ก์ ๋ก ์บก์ฒํ์ฌ, ํด๋น ์ํ๊ฐ๋ง ์ ํํ ์ ๋ฐ์ดํธํ๋๋ก ๊ตฌํ๋์ด ์๋ค.\r\n\r\n์ ๋ฆฌํ์๋ฉด, ํด๋ก์ ๋ `setState`๊ฐ ํจ์๊ฐ ์ ์ธ๋ ๋น์์ ํ๊ฒฝ์ ์ ์งํ๊ฒ ํด์ฃผ๊ธฐ ๋๋ฌธ์ **์ปดํฌ๋ํธ๊ฐ ๋งค๋ฒ ์ฌ์คํ๋ ๋๋ง๋ค ์ํ๊ฐ ์ด๊ธฐํ๋๋ ๊ฒ์ ๋ฐฉ์ง**ํ๊ณ , ์ํ๊ฐ ์ฌ๋ฐ๋ฅด๊ฒ ์ ์ง๋ ์ ์๊ฒ ํด์ค๋ค.\r\n\r\n**์ฐธ๊ณ **\r\n\r\n[์์ ] ๋ชจ๋ ๋ฆฌ์กํธ Deep Dive\r\n"}]},"__N_SSG":true} |
File renamed without changes.
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"pageProps":{"tags":["react","JavaScript","deepdive","useState","hook","ํด๋ก์ ","์๋ช ์ฃผ๊ธฐ","Virtual DOM","Algorithm","๋ฐฑ์ค","gold","dfs","ํ๋ก๊ทธ๋๋จธ์ค","lv3","bfs","์ด๋ถํ์"],"currentTag":"๋ฐฑ์ค","posts":[{"slug":"posts/Algorithm/[๋ฐฑ์ค gold 5] ๋น๋ฐ ํธ์","title":"[๋ฐฑ์ค gold 5] ๋น๋ฐ ํธ์","date":"2024-08-09T17:05:24.000Z","image":"index.png","summary":"dfs์ ํ์ฉํ ํ๋ก๊ทธ๋๋จธ์ค [level 3] ์ง๊ฒ๋ค๋ฆฌ ๊ฑด๋๊ธฐ ๋ฌธ์ ํ์ด ์ ๋๋ค.","tags":["Algorithm","๋ฐฑ์ค","gold","dfs"],"content":"# [gold 3] ๋น๋ฐ ํธ์\r\n\r\n## ๋ฌธ์ ์ค๋ช \r\n\r\n์น๋ฅด๋ณด๊ธฐ ๋น๋ฉ์ 11์ธต๋ถํฐ NN์ธต๊น์ง ์ด์ฉ์ด ๊ฐ๋ฅํ ์๋ฆฌ๋ฒ ์ดํฐ๊ฐ ์๋ค. ์๋ฆฌ๋ฒ ์ดํฐ์ ์ธต์๋ฅผ ๋ณด์ฌ์ฃผ๋ ๋์คํ๋ ์ด์๋ KK ์๋ฆฌ์ ์๊ฐ ๋ณด์ธ๋ค. ์๋ 00์ผ๋ก ์์ํ ์๋ ์๋ค. 00๋ถํฐ 99๊น์ง์ ๊ฐ ์ซ์๊ฐ ๋์คํ๋ ์ด์ ๋ณด์ด๋ ๋ฐฉ์์ ์๋์ ๊ฐ๋ค. ๊ฐ ์ซ์๋ 7๊ฐ์ ํ์๋ฑ ์ค์ ์ผ๋ถ์ ๋ถ์ด ๋ค์ด์ค๋ฉด์ ํํ๋๋ค.\r\n\r\n![](1.png)\r\n\r\n์๋ฅผ ๋ค์ด K=4K=4์ธ ๊ฒฝ์ฐ์ 16801680์ธต๊ณผ 501501์ธต์ ์๋์ ๊ฐ์ด ๋ณด์ธ๋ค.\r\n\r\n![](2.png)\r\n\r\n\r\n\r\n๋น๋ฐ ํธ์์ ์น๋ฅด๋ณด๊ธฐ ๋น๋ฉ์ ์๋ฆฌ๋ฒ ์ดํฐ ๋์คํ๋ ์ด์ LED ์ค์์ ์ต์ 11๊ฐ, ์ต๋ PP๊ฐ๋ฅผ ๋ฐ์ ์ํฌ ๊ณํ์ ์ธ์ฐ๊ณ ์๋ค. ๋ฐ์ ์ด๋ ์ผ์ง ๋ถ๋ถ์ ๋๊ณ , ๊บผ์ง ๋ถ๋ถ์ ์ผ๋ ๊ฒ์ ์๋ฏธํ๋ค. ์๋ฅผ ๋ค์ด ์ซ์ 11์ 22๋ก ๋ฐ๊พธ๋ ค๋ฉด ์ด 5๊ฐ์ LED๋ฅผ ๋ฐ์ ์์ผ์ผ ํ๋ค. ๋ํ ๋ฐ์ ์ดํ์ ๋์คํ๋ ์ด์ ์ฌ๋ฐ๋ฅธ ์๊ฐ ๋ณด์ฌ์ง๋ฉด์ 11 ์ด์ NN ์ดํ๊ฐ ๋๋๋ก ๋ฐ๊ฟ์ ์ฌ๋๋ค์ ํท๊ฐ๋ฆฌ๊ฒ ํ ์์ ์ด๋ค. ์น๋ฅด๋ณด๊ธฐ๋ฅผ ์ฌ๋ํ๋ ๋ชจ์์ ํ์์ธ ๋น์ ์ ํธ์ ๋น๋ฐ์ ํ๋์ ๋ฏธ๋ฆฌ ํ์ ํด์ ํผ์ญ์ ๋ด์ฃผ๊ณ ์ ํ๋ค. ํ์ฌ ์๋ฆฌ๋ฒ ์ดํฐ๊ฐ ์ค์ ๋ก๋ XX์ธต์ ๋ฉ์ถฐ์์ ๋, ํธ์์ด๊ฐ ๋ฐ์ ์ํฌ LED๋ฅผ ๊ณ ๋ฅผ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐํด๋ณด์.\r\n\r\n### ์ ๋ ฅ\r\n\r\nโN,K,P,XN,K,P,X ๊ฐ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด ์ฒซ์งธ ์ค์ ์ฃผ์ด์ง๋ค.\r\n\r\n### ์ถ๋ ฅ\r\n\r\nํธ์ ๋น๋ฐ์ด ์๋ฆฌ๋ฒ ์ดํฐ LED๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ๋ฐ์ ์ํฌ ์ ์๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ณ์ฐํด๋ณด์.\r\n\r\n## ์ ๊ทผ ๋ฐฉ๋ฒ\r\n\r\n์ซ์์ ๋ฐ๋ฅธ LED๋ฅผ ๋จผ์ ๊ตฌํํ๊ธฐ ์ํด, \r\n\r\n๊ทธ๋ฆผ๊ณผ ๊ฐ์ด, ๊ฐ LED์ ์์น์ ๋ฒํธ๋ฅผ ๋งค๊ฒจ, ๋ฐฐ์ด์์ ๊ทธ ๋ฒํธ๊ฐ ์์ผ๋ฉด ๋ถ์ด ์ผ์ ธ์๋ ๊ฒ์ผ๋ก, ์๋ค๋ฉด ๋ถ์ด ๊บผ์ ธ ์๋ ๊ฒ์ผ๋ก ํํํ์๋ค.\r\n<img src=\"3.png\" alt=\"์ด๋ฏธ์ง ์ค๋ช \" width=\"200\" height=\"300\" />\r\n\r\n\r\n๊ทธ๋ฆผ์ฒ๋ผ 0์ ํํํ๋ฉด 3๋ฒ์๋ฆฌ๋ฅผ ์ ์ธํ ๋ชจ๋ ๋ฒํธ๊ฐ ์ผ์ ธ์์ผ๋ฏ๋ก, `[0,1,2,4,5,6]`์ผ๋ก ํํ ํ ์ ์๋ค.\r\n\r\n๊ฐ ๋ฒํธ๋ฅผ ์ด์ ๊ฐ์ด ๋ฐ๊พธ์ด numbers ๋ฐฐ์ด์ ์ ์ฅํ๋ฉด, ์ด์ ๊ฐ๋ค.\r\n```python\r\nnumbers = [[0,1,2,4,5,6],[2,5],[0,2,3,4,6],[0,2,3,5,6],[1,2,3,5],[0,1,3,5,6],[0,1,3,4,5,6],[0,2,5],[0,1,2,3,4,5,6],[0,1,2,3,5,6]];\r\n```\r\n์ดํ ๋ง์ผ ํ์ฌ ๋ฒํธ์ ๋น๊ตํ ๋ฒํธ์ LED๋ฅผ ๋น๊ตํ๊ธฐ ์ํด ์๋ก์ ์ฐจ์งํฉ์ ๋ํด์ ๋ ๋ฒํธ๊ฐ ์๋ก๋ฅผ ๋น๊ตํ์ ๋, ๊ฐ์ง๊ณ ์๋ ํน์ ์๋ ๋ฒํธ์ ๊ฐ์๋ฅผ ๊ตฌํ๋ค.\r\n```python\r\ncur_count = len(list(cur.difference(compare))) + len(list(compare.difference(cur)));\r\n```\r\n์ด์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ฐ์ ์ํฌ LED์ ๊ฐ์๋ฅผ ๊ตฌํ๊ณ , dfs๋ฅผ ํ์ฉํ์ฌ x์ ๋ชจ๋ ์๋ฆฟ์๋ฅผ ์ํํ์ฌ p๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๊ฒ LED๋ฅผ ๋ฐ์ ํ๋ฉด์, n๋ณด๋ค ๋ฒํธ๊ฐ ์๊ฑฐ๋ ๊ฐ๋ค๋ฉด result๋ฅผ 1 ์ฆ๊ฐ ์ํจ๋ค.\r\n\r\n์ ์ฒด์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.\r\n```python\r\nimport sys;\r\ninput = sys.stdin.readline;\r\n\r\nn,k,p,x = map(int, input().split());\r\nresult = 0;\r\nnumbers = [[0,1,2,4,5,6],[2,5],[0,2,3,4,6],[0,2,3,5,6],[1,2,3,5],[0,1,3,5,6],[0,1,3,4,5,6],[0,2,5],[0,1,2,3,4,5,6],[0,1,2,3,5,6]];\r\n\r\nx = list(str(x));\r\nif len(x) != k:\r\n\t# k ๋ณด๋ค ์๋ฆฟ์ x๊ฐ ์๋ฆฟ์๊ฐ ์๋ค๋ฉด ์์ ๋งํผ ์์ 0์ ์ฑ์์ฃผ์ด์ผ ํ๋ค.\r\n for _ in range(k-len(x)):\r\n x.insert(0,'0');\r\n \r\ndef dfs(index,count,st):\r\n global result;\r\n if count>p:\r\n return;\r\n if index == k:\r\n if 0<count<=p and 0<int(st)<=n:\r\n result += 1;\r\n return;\r\n cur = set(numbers[int(x[index])]);\r\n for i in range(len(numbers)):\r\n compare = set(numbers[i]);\r\n cur_count = len(list(cur.difference(compare))) + len(list(compare.difference(cur)));\r\n if count + cur_count <=p:\r\n dfs(index+1,count + cur_count,st + str(i));\r\ndfs(0,0,'');\r\nprint(result);\r\n```\r\n\r\n## ๋๋ ์ \r\n์ฌ์ค ์ด ๋ฌธ์ ์ ๊ด๋ จํ์ฌ ๋ค๋ฅธ ํ์ด๋ ๋ง์ด ์ฐพ์๋ณด์๊ณ , ๋์ฑ ์๊ฐ์ด ์ ๊ฒ ๊ฑธ๋ฆฌ๋ ํ์ด๋ ๋ง์์ด์ ์๊ฐ ์ด๊ณผ๋ ๋์ง ์์์ง๋ง ๋ค์ ๋นํจ์จ์ ์ธ ํ์ด์๋ค๋ ์๊ฐ์ด ๋ค์๋ค.\r\n\r\n๋ํ ์ ํ ์ฌํญ์ค `๋ํ ๋ฐ์ ์ดํ์ ๋์คํ๋ ์ด์ ์ฌ๋ฐ๋ฅธ ์๊ฐ ๋ณด์ฌ์ง๋ฉด์ 1 ์ด์ N ์ดํ๊ฐ ๋๋๋ก ๋ฐ๊ฟ์ ์ฌ๋๋ค์ ํท๊ฐ๋ฆฌ๊ฒ ํ ์์ ์ด๋ค.`๋ฅผ ๋ชป๋ด์ ๊ณ์ `0<=int(st)<=n`๋ก ์กฐ๊ฑด์ ์ค์ ํด๋๊ณ ํค๋งธ์๋ค. ๋ฌธ์ ์ ์กฐ๊ฑด์ ์ ์ฌํ ๋ณด๋ ์ต๊ด์ ๋ค์ฌ์ผ ํจ์ ๋ค์ํ๋ฒ ๋๋๋ค."}]},"__N_SSG":true} |
Oops, something went wrong.