SlideShare une entreprise Scribd logo
1  sur  8
Problema săriturii calului

ELEV: Zirbo Alin

CLASA:a XI-a B
Enunţul problemei
Se dă o tablă de şah cu dimensiunea n*n. Un
cal se găseşte în linia 1 şi coloana 1. Găsiţi un şir de
mutări ale calului astfel încât acesta să acopere
întreaga tablă fără a trece
printr-o căsuţă de două ori.
Pentru n=5 avem o tablă de
dimensiunea 5 linii şi 5 coloane
Algoritmul de rezolvare
Cele 8 poziţii în care poate sări calul sunt:

i-2
j-1

i-2
j+1

i-1
j-2

i-1
j+2

i,j
i+1
j-2

i+1
j+2
i+2
j-1

i+2
j+1
 Toate elementele la început sunt egale cu 0 asta însemnând că







iniţial nu s-a trecut pe la nici un pătrat. Calul porneşte de pe poziţia
a[1,1] care va fi egal cu 1.
Numărul soluţiilor notat cu ns este la început 0 şi se măreşte cu 1
când se apelează procedura de afişare a matricii.
De la elementele a[i,j] sare dacă poate într-una dintre cele 8 direcţii
adică dacă trebuie să nu sară în afara tablei şi să nu mai fi trecut pe
acolo.
Dacă sare pe una din cele 8 poziţii se procedează astfel:
1. Elementele din matrice de pe acea poziţie primeşte ca valoare
pas.
2. Dacă pasul la care s-a ajuns este n*n, adică pas=n*n, atunci se
afişează matricea altfel se autoapelează asupra acestui element.
3. Elementele din matrice de pe acea poziţie primeşte valoarea 0.
Pentru a ajunge de la a[i,j] la una din cele 8 poziţii se folosesc
vectorii de direcţie: pentru linie di (-2,-1,1,2,2,1,-1,-2), pentru
coloană dj (1,2,2,1,-1,-2,-2,-1).
Implementarea algoritmului
ns - numărul soluţiilor
n – numărul de linii şi coloane
a – matrice pătratică
i,j – indicii elementului la care s-a ajuns
di – vector de poziţie pentru linie
dj – vector de poziţie pentru coloană
pas – pasul la care s-a ajuns
k – direcţiile
i1, j1 – coordonatele noii poziţii
Programul:
Program cal;
Type sir=array[1..8] of integer;
mat=array[1..20,1..20] of integer;
const di: sir=(-2,-1,1,2,2,1,-1,-2);
dj: sir=(1,2,2,1,-1,-2,-2,-1);
Var a:mat;
i,j,n,ns,pas:integer;
Procedure afis_mat;
Begin
ns:=ns+1;
writeln (‘solutia cu numarul’, ns, ‘este:’);
for i:=1 to n do begin
for j:=1 to n do
write(a[i,j]:3);writeln;end;
end;
procedure calul(var a:mat; i,j,pas:integer);
var k,i1,j1:integer;
cond:boolean;
begin
for k:=1 to 8 do begin
i1:=i+di[k];
j1:=j+dj[k];
cond:=(i1 in [1..n]) and (j1 in [1..n]) and (a[i1,j1]=0);
if cond then begin
a[i1,j1]:=pas;
if pas=n*n then afis_mat
else calul(a,i1,j1,pas+1);
a[i1,j1]:=0; end;
end;
Begin
write(‘n=‘);readln(n);
For i:=1 to n do
for j:=1 to n do a[i,j]:=0;
a[1,1]:=1; ns:=0;
pas:=2;
calul(a,1,1,pas);
readln;
End.

Contenu connexe

En vedette

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

En vedette (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Problema calului-1232691710728721-2 (1)

  • 1. Problema săriturii calului ELEV: Zirbo Alin CLASA:a XI-a B
  • 2. Enunţul problemei Se dă o tablă de şah cu dimensiunea n*n. Un cal se găseşte în linia 1 şi coloana 1. Găsiţi un şir de mutări ale calului astfel încât acesta să acopere întreaga tablă fără a trece printr-o căsuţă de două ori.
  • 3. Pentru n=5 avem o tablă de dimensiunea 5 linii şi 5 coloane
  • 4. Algoritmul de rezolvare Cele 8 poziţii în care poate sări calul sunt: i-2 j-1 i-2 j+1 i-1 j-2 i-1 j+2 i,j i+1 j-2 i+1 j+2 i+2 j-1 i+2 j+1
  • 5.  Toate elementele la început sunt egale cu 0 asta însemnând că     iniţial nu s-a trecut pe la nici un pătrat. Calul porneşte de pe poziţia a[1,1] care va fi egal cu 1. Numărul soluţiilor notat cu ns este la început 0 şi se măreşte cu 1 când se apelează procedura de afişare a matricii. De la elementele a[i,j] sare dacă poate într-una dintre cele 8 direcţii adică dacă trebuie să nu sară în afara tablei şi să nu mai fi trecut pe acolo. Dacă sare pe una din cele 8 poziţii se procedează astfel: 1. Elementele din matrice de pe acea poziţie primeşte ca valoare pas. 2. Dacă pasul la care s-a ajuns este n*n, adică pas=n*n, atunci se afişează matricea altfel se autoapelează asupra acestui element. 3. Elementele din matrice de pe acea poziţie primeşte valoarea 0. Pentru a ajunge de la a[i,j] la una din cele 8 poziţii se folosesc vectorii de direcţie: pentru linie di (-2,-1,1,2,2,1,-1,-2), pentru coloană dj (1,2,2,1,-1,-2,-2,-1).
  • 6. Implementarea algoritmului ns - numărul soluţiilor n – numărul de linii şi coloane a – matrice pătratică i,j – indicii elementului la care s-a ajuns di – vector de poziţie pentru linie dj – vector de poziţie pentru coloană pas – pasul la care s-a ajuns k – direcţiile i1, j1 – coordonatele noii poziţii
  • 7. Programul: Program cal; Type sir=array[1..8] of integer; mat=array[1..20,1..20] of integer; const di: sir=(-2,-1,1,2,2,1,-1,-2); dj: sir=(1,2,2,1,-1,-2,-2,-1); Var a:mat; i,j,n,ns,pas:integer; Procedure afis_mat; Begin ns:=ns+1; writeln (‘solutia cu numarul’, ns, ‘este:’); for i:=1 to n do begin for j:=1 to n do write(a[i,j]:3);writeln;end; end;
  • 8. procedure calul(var a:mat; i,j,pas:integer); var k,i1,j1:integer; cond:boolean; begin for k:=1 to 8 do begin i1:=i+di[k]; j1:=j+dj[k]; cond:=(i1 in [1..n]) and (j1 in [1..n]) and (a[i1,j1]=0); if cond then begin a[i1,j1]:=pas; if pas=n*n then afis_mat else calul(a,i1,j1,pas+1); a[i1,j1]:=0; end; end; Begin write(‘n=‘);readln(n); For i:=1 to n do for j:=1 to n do a[i,j]:=0; a[1,1]:=1; ns:=0; pas:=2; calul(a,1,1,pas); readln; End.